From 79c4492abbf1b678872e15a08b26eae52328b91d Mon Sep 17 00:00:00 2001 From: contrapunctus Date: Tue, 19 May 2020 01:21:24 +0530 Subject: [PATCH 1/2] ts-human-format-duration - use singular unit names when values are 1 --- test/test.el | 2 +- ts.el | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/test/test.el b/test/test.el index 6a6de3d..86ed516 100644 --- a/test/test.el +++ b/test/test.el @@ -241,7 +241,7 @@ (let* ((now (ts-now)) (past (ts-adjust 'day -400 'hour -2 'minute -1 'second -5 now))) (should (equal (ts-human-format-duration (ts-difference now past)) - "1 years, 35 days, 2 hours, 1 minutes, 5 seconds")) + "1 year, 35 days, 2 hours, 1 minute, 5 seconds")) (should (equal (ts-human-format-duration (ts-difference now past) 'abbr) "1y35d2h1m5s")))) diff --git a/ts.el b/ts.el index 875a3c3..83f3b71 100644 --- a/ts.el +++ b/ts.el @@ -465,7 +465,9 @@ leap years, leap seconds, etc." (if abbreviate "" " ") (if abbreviate ,(substring (symbol-name place) 0 1) - ,(symbol-name place))))) + (if (= ,place 1) + (s-chop-suffix "s" ,(symbol-name place)) + ,(symbol-name place)))))) (join-places (&rest places) ;; Return string joining the names and values of PLACES. `(->> (list ,@(cl-loop for place in places From 59f2a0ec87b01d21252b5018a1816af9819966d5 Mon Sep 17 00:00:00 2001 From: contrapunctus Date: Tue, 19 May 2020 09:54:59 +0530 Subject: [PATCH 2/2] ts-human-format-duration - add test cases for singulars and plurals --- test/test.el | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/test.el b/test/test.el index 86ed516..c3c02bc 100644 --- a/test/test.el +++ b/test/test.el @@ -238,12 +238,18 @@ ;;;;; Duration (ert-deftest ts-human-format-duration () - (let* ((now (ts-now)) - (past (ts-adjust 'day -400 'hour -2 'minute -1 'second -5 now))) + (let* ((now (ts-now)) + (past (ts-adjust 'day -400 'hour -2 'minute -1 'second -5 now)) + (past-1 (ts-adjust 'day -366 'hour -1 'minute -1 'second -1 now)) + (past-2 (ts-adjust 'day (- (+ 2 (* 365 2))) 'hour -2 'minute -2 'second -2 now))) (should (equal (ts-human-format-duration (ts-difference now past)) "1 year, 35 days, 2 hours, 1 minute, 5 seconds")) (should (equal (ts-human-format-duration (ts-difference now past) 'abbr) - "1y35d2h1m5s")))) + "1y35d2h1m5s")) + (should (equal (ts-human-format-duration (ts-difference now past-1)) + "1 year, 1 day, 1 hour, 1 minute, 1 second")) + (should (equal (ts-human-format-duration (ts-difference now past-2)) + "2 years, 2 days, 2 hours, 2 minutes, 2 seconds")))) ;;;;; Formatting