Add helper functions to display human readable dates
This commit is contained in:
parent
5f2905e38f
commit
14b28b767f
3 changed files with 35 additions and 25 deletions
2
Makefile
2
Makefile
|
@ -1,4 +1,4 @@
|
||||||
ELPA_DEPENDENCIES=package-lint transient dash
|
ELPA_DEPENDENCIES=package-lint transient dash ts
|
||||||
|
|
||||||
ELPA_ARCHIVES=melpa-stable gnu
|
ELPA_ARCHIVES=melpa-stable gnu
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
(require 'dash)
|
(require 'dash)
|
||||||
(require 'transient)
|
(require 'transient)
|
||||||
(require 'timezone)
|
(require 'timezone)
|
||||||
|
(require 'ts)
|
||||||
|
|
||||||
(defgroup taskwarrior nil "An emacs frontend to taskwarrior.")
|
(defgroup taskwarrior nil "An emacs frontend to taskwarrior.")
|
||||||
|
|
||||||
|
@ -261,7 +262,7 @@
|
||||||
(let* ((id (format "%s" (alist-get 'id entry)))
|
(let* ((id (format "%s" (alist-get 'id entry)))
|
||||||
(urgency (format "%0.2f" (alist-get 'urgency entry)))
|
(urgency (format "%0.2f" (alist-get 'urgency entry)))
|
||||||
(priority (format " %s " (or (alist-get 'priority entry) "")))
|
(priority (format " %s " (or (alist-get 'priority entry) "")))
|
||||||
(due (format "%s" (or (taskwarrior--parse-date (alist-get 'due entry)) "")))
|
(due (format "%s" (or (taskwarrior--timespan-days (alist-get 'due entry)) "")))
|
||||||
(annotations (format "%d" (length (or (alist-get 'annotations entry) '()))))
|
(annotations (format "%d" (length (or (alist-get 'annotations entry) '()))))
|
||||||
(project (or (alist-get 'project entry) ""))
|
(project (or (alist-get 'project entry) ""))
|
||||||
(tags (or (taskwarrior--concat-tag-list (alist-get 'tags entry)) ""))
|
(tags (or (taskwarrior--concat-tag-list (alist-get 'tags entry)) ""))
|
||||||
|
@ -472,25 +473,37 @@
|
||||||
("w" "wait" taskwarrior-set-wait)
|
("w" "wait" taskwarrior-set-wait)
|
||||||
("u" "until" taskwarrior-set-untl)]])
|
("u" "until" taskwarrior-set-untl)]])
|
||||||
|
|
||||||
(defun taskwarrior--parse-date (timestamp &optional format)
|
(defun taskwarrior--reformat-date (twdate)
|
||||||
"Parse a taskwarrior TIMESTAMP into a readable string with FORMAT."
|
"Reformat a taskwarrior TWDATE into a readable string."
|
||||||
(let* ((format (or format :date))
|
(let ((regex (rx
|
||||||
(datetime (timezone-parse-date timestamp))
|
(seq
|
||||||
(year (elt datetime 0))
|
(submatch (= 4 digit))
|
||||||
(month (elt datetime 1))
|
(submatch (= 2 digit))
|
||||||
(day (elt datetime 2))
|
(submatch (= 2 digit))
|
||||||
(time (timezone-parse-time (elt datetime 3)))
|
"T"
|
||||||
(hour (elt time 0))
|
(submatch (= 2 digit))
|
||||||
(minute (elt time 1))
|
(submatch (= 2 digit))
|
||||||
(second (elt time 2)))
|
(submatch (= 2 digit))
|
||||||
(cond
|
"Z"
|
||||||
((eq timestamp nil)
|
eol))))
|
||||||
nil)
|
(when (string-match regex twdate)
|
||||||
((eq format :date)
|
(format "%s-%s-%s %s:%s:%s"
|
||||||
(format "%s-%s-%s" year month day hour minute second))
|
(match-string 1 twdate)
|
||||||
((eq format :datetime)
|
(match-string 2 twdate)
|
||||||
(format "%s-%s-%s %s:%s:%s" year month day hour minute second)))))
|
(match-string 3 twdate)
|
||||||
|
(match-string 4 twdate)
|
||||||
|
(match-string 5 twdate)
|
||||||
|
(match-string 6 twdate)))))
|
||||||
|
|
||||||
|
(defun taskwarrior--parse-date (twdate)
|
||||||
|
(ts-parse (taskwarrior--reformat-date twdate)))
|
||||||
|
|
||||||
|
(defun taskwarrior--timespan-days (twdate)
|
||||||
|
(if (eq twdate nil)
|
||||||
|
nil
|
||||||
|
(let ((now (ts-now))
|
||||||
|
(datetime (taskwarrior--parse-date twdate)))
|
||||||
|
(format-seconds "%D" (- (ts-unix datetime) (ts-unix now))))))
|
||||||
|
|
||||||
(provide 'taskwarrior)
|
(provide 'taskwarrior)
|
||||||
;;; taskwarrior.el ends here
|
;;; taskwarrior.el ends here
|
||||||
|
|
|
@ -23,11 +23,8 @@
|
||||||
(should (string= (taskwarrior--parse-created-task-id "Created task 1000.") "1000"))
|
(should (string= (taskwarrior--parse-created-task-id "Created task 1000.") "1000"))
|
||||||
(should (string= (taskwarrior--parse-created-task-id "Some invalid input") nil)))
|
(should (string= (taskwarrior--parse-created-task-id "Some invalid input") nil)))
|
||||||
|
|
||||||
(ert-deftest taskwarrior--parse-date ()
|
(ert-deftest taskwarrior--reformat-date ()
|
||||||
(should (string= (taskwarrior--parse-date "20191108T225959Z") "2019-11-08"))
|
(should (string= (taskwarrior--reformat-date "20191108T225959Z") "2019-11-08 22:59:59")))
|
||||||
(should (string= (taskwarrior--parse-date "20191108T225959Z" :date) "2019-11-08"))
|
|
||||||
(should (string= (taskwarrior--parse-date "20191108T225959Z" :datetime) "2019-11-08 22:59:59")))
|
|
||||||
|
|
||||||
|
|
||||||
(provide 'taskwarrior-test)
|
(provide 'taskwarrior-test)
|
||||||
;;; taskwarrior-test.el ends here
|
;;; taskwarrior-test.el ends here
|
||||||
|
|
Loading…
Reference in a new issue