diff --git a/elfeed-lib.el b/elfeed-lib.el index b9973de..5168a39 100644 --- a/elfeed-lib.el +++ b/elfeed-lib.el @@ -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))) diff --git a/tests/elfeed-lib-tests.el b/tests/elfeed-lib-tests.el index e2398b9..2898e2f 100644 --- a/tests/elfeed-lib-tests.el +++ b/tests/elfeed-lib-tests.el @@ -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"))