From 3e5dea2aaf0d488b0f6d292cffc5d9da9dcf0321 Mon Sep 17 00:00:00 2001 From: Patrick Winter Date: Thu, 7 Nov 2019 19:01:18 +0100 Subject: [PATCH] Add helper function to parse taskwarrior timestamps --- taskwarrior.el | 18 ++++++++++++++++++ test/taskwarrior-test.el | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/taskwarrior.el b/taskwarrior.el index ddb0429..5618eb3 100644 --- a/taskwarrior.el +++ b/taskwarrior.el @@ -471,5 +471,23 @@ ("w" "wait" taskwarrior-set-wait) ("u" "until" taskwarrior-set-untl)]]) +(defun taskwarrior--parse-date (timestamp &optional format) + "Parse a taskwarrior TIMESTAMP into a readable string with FORMAT." + (let* ((format (or format :date)) + (datetime (timezone-parse-date timestamp)) + (year (elt datetime 0)) + (month (elt datetime 1)) + (day (elt datetime 2)) + (time (timezone-parse-time (elt datetime 3))) + (hour (elt time 0)) + (minute (elt time 1)) + (second (elt time 2))) + (cond + ((eq format :date) + (format "%s-%s-%s" day month year hour minute second)) + ((eq format :datetime) + (format "%s-%s-%s %s:%s:%s" day month year hour minute second))))) + + (provide 'taskwarrior) ;;; taskwarrior.el ends here diff --git a/test/taskwarrior-test.el b/test/taskwarrior-test.el index 3dfb10c..0043c5a 100644 --- a/test/taskwarrior-test.el +++ b/test/taskwarrior-test.el @@ -23,6 +23,11 @@ (should (string= (taskwarrior--parse-created-task-id "Created task 1000.") "1000")) (should (string= (taskwarrior--parse-created-task-id "Some invalid input") nil))) +(ert-deftest taskwarrior--parse-date () + (should (string= (taskwarrior--parse-date "20191108T225959Z") "08-11-2019")) + (should (string= (taskwarrior--parse-date "20191108T225959Z" :date) "08-11-2019")) + (should (string= (taskwarrior--parse-date "20191108T225959Z" :datetime) "08-11-2019 22:59:59"))) + (provide 'taskwarrior-test) ;;; taskwarrior-test.el ends here