Skip to content

Commit

Permalink
Fix elfeed-time-duration-absolute for Emacs 24.3
Browse files Browse the repository at this point in the history
The time-subtract function in Emacs 24.5 and earlier does not support
float seconds. So, instead, always use float seconds and replace the
time-subtract with a plain substraction (-). Emacs' encoding of time
using a list of small integers was a mistake since double precision
floats provide more than enough precision (53 bits) to store these time
values.

Also added an optional argument to support testing since overriding
float-time via cl-letf wasn't working.
  • Loading branch information
skeeto committed Aug 24, 2019
1 parent 8743343 commit c9350c8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
9 changes: 6 additions & 3 deletions elfeed-lib.el
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,16 @@
(end-of-line)
(delete-region start (point))))

(defun elfeed-time-duration (time)
(defun elfeed-time-duration (time &optional now)
"Turn a time expression into a number of seconds. Uses
`timer-duration' but allows a bit more flair."
`timer-duration' but allows a bit more flair.
If `now' is non-nil, use it as the current time (`float-time'). This
is mostly useful for testing."
(cond
((numberp time) time)
((let ((iso-time (elfeed-parse-simple-iso-8601 time)))
(when iso-time (float-time (time-subtract (current-time) iso-time)))))
(when iso-time (- (or now (float-time)) iso-time))))
((string-match-p "[[:alpha:]]" time)
(let* ((clean (replace-regexp-in-string "\\(ago\\|old\\|-\\)" " " time))
(duration (timer-duration clean)))
Expand Down
24 changes: 15 additions & 9 deletions tests/elfeed-lib-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@

(ert-deftest elfeed-time-duration-absolute ()
;; fixed time for testing: assume U.S. eastern
(cl-letf (((symbol-function 'current-time)
(lambda () (encode-time 0 20 13 24 6 2019 (* -1 4 60 60)))))
(let ((now (float-time (encode-time 0 20 13 24 6 2019 (* -1 4 60 60)))))
;; "2019-06-24T13:20:00-04:00" is "2019-06-24T17:20:00Z" so 17h 20mins is
;; the time difference:
(should (= (+ (* 17 60 60) (* 20 60)) (elfeed-time-duration "2019-06-24")))
(should (= (* 10 60) (elfeed-time-duration "2019-06-24T17:10")))
(should (= (* 10 60) (elfeed-time-duration "2019-06-24T17:10:00")))
(should (= (+ (* 9 60) 30) (elfeed-time-duration "2019-06-24T17:10:30")))
(should (= (+ (* 9 60) 30) (elfeed-time-duration "2019-06-24T17:10:30Z")))
(should (= (+ (* 9 60) 30) (elfeed-time-duration "2019-06-24T17:10:30+00:00")))
(should (= (+ (* 9 60) 30) (elfeed-time-duration "20190624T17:10:30+00:00")))))
(should (= (+ (* 17 60 60) (* 20 60))
(elfeed-time-duration "2019-06-24" now)))
(should (= (* 10 60)
(elfeed-time-duration "2019-06-24T17:10" now)))
(should (= (* 10 60)
(elfeed-time-duration "2019-06-24T17:10:00" now)))
(should (= (+ (* 9 60) 30)
(elfeed-time-duration "2019-06-24T17:10:30" now)))
(should (= (+ (* 9 60) 30)
(elfeed-time-duration "2019-06-24T17:10:30Z" now)))
(should (= (+ (* 9 60) 30)
(elfeed-time-duration "2019-06-24T17:10:30+00:00" now)))
(should (= (+ (* 9 60) 30)
(elfeed-time-duration "20190624T17:10:30+00:00" now)))))

(ert-deftest elfeed-format-column ()
(should (string= (elfeed-format-column "foo" 10 :right) " foo"))
Expand Down

0 comments on commit c9350c8

Please sign in to comment.