From 027db4ac4757830cc5e83cc966d3b5cfbb50b41c Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sat, 16 Nov 2024 05:24:39 +0800 Subject: [PATCH 1/3] fix reported difference in datetime when passed in a date in the future; we do this by simply "coercing" the :now-dt argument to be ~at midnight as well~ when then-dt is a java.time.LocalDate --- src/clj_commons/humanize.cljc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/clj_commons/humanize.cljc b/src/clj_commons/humanize.cljc index 3627716..ee05e60 100644 --- a/src/clj_commons/humanize.cljc +++ b/src/clj_commons/humanize.cljc @@ -4,6 +4,7 @@ [clj-commons.humanize.inflect :refer [pluralize-noun in?]] [clj-commons.humanize.time-convert :refer [coerce-to-local-date-time]] [cljc.java-time.duration :as jt.duration] + [cljc.java-time.extn.predicates :as jt.predicates] [cljc.java-time.local-date-time :as jt.ldt] [clojure.string :as string :refer [join]] #?@(:cljs [[goog.string :as gstring] @@ -292,8 +293,16 @@ :or {now-dt (jt.ldt/now) suffix "ago" prefix "in"}}] - (let [then-dt (coerce-to-local-date-time then-dt) + (let [local-date? (jt.predicates/local-date? then-dt) + then-dt (coerce-to-local-date-time then-dt) now-dt (coerce-to-local-date-time now-dt) + now-dt (if local-date? + (-> now-dt + (jt.ldt/with-hour 0) + (jt.ldt/with-minute 0) + (jt.ldt/with-second 0) + (jt.ldt/with-nano 0)) + now-dt) future-time? (jt.ldt/is-after then-dt now-dt) ;; get the Duration between the two times time-between (-> (jt.duration/between then-dt now-dt) @@ -342,6 +351,9 @@ future-time? (str prefix " a moment") + local-date? + "today" + :else (str "a moment " suffix)))) From 7bd8d8fa81b61762820105166145eedb1cbbb329 Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sat, 16 Nov 2024 11:26:00 +0800 Subject: [PATCH 2/3] add tests for datetime handling of date arguments --- test/clj_commons/humanize_test.cljc | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/test/clj_commons/humanize_test.cljc b/test/clj_commons/humanize_test.cljc index 8346528..55f7278 100644 --- a/test/clj_commons/humanize_test.cljc +++ b/test/clj_commons/humanize_test.cljc @@ -6,6 +6,7 @@ duration] :as h] [clojure.math :as math] + [cljc.java-time.local-date :as jt.ld] [cljc.java-time.local-date-time :as jt.ldt])) (def ^:private expt math/pow) @@ -216,7 +217,8 @@ (deftest datetime-test (let [t1-str "2022-01-01T01:00:00" - t1 (jt.ldt/parse t1-str)] + t1 (jt.ldt/parse t1-str) + ld-now (jt.ld/now)] (is (= "a moment ago" (datetime (jt.ldt/now))) ":now-dt is optional") @@ -256,7 +258,20 @@ (datetime (jt.ldt/plus-years t1 1) :now-dt t1 :suffix "foo")) - "suffix for a time in the past does nothing")))) + "suffix for a time in the past does nothing")) + (testing "datetime handles date arguments intuitively and without being affected by the specific time now" + (is (= "today" + (datetime (jt.ld/now))) + "LocalDate/now is today") + (is (and (= "in 1 day" + (datetime (jt.ld/plus-days ld-now 1))) + (= "1 day ago" + (datetime (jt.ld/minus-days ld-now 1))) + (= "in 3 days" + (datetime (jt.ld/plus-days ld-now 3))) + (= "3 days ago" + (datetime (jt.ld/minus-days ld-now 3)))) + "equidistant dates in the future vs past are accounted for in the same way")))) (deftest durations (testing "duration to terms" From f9ce047cb2d192ea96ba3ca12c63201bd40faefc Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sat, 16 Nov 2024 11:43:52 +0800 Subject: [PATCH 3/3] update datetime tests to include ldt :dt-now argument --- test/clj_commons/humanize_test.cljc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/clj_commons/humanize_test.cljc b/test/clj_commons/humanize_test.cljc index 55f7278..2d1c6f0 100644 --- a/test/clj_commons/humanize_test.cljc +++ b/test/clj_commons/humanize_test.cljc @@ -261,8 +261,12 @@ "suffix for a time in the past does nothing")) (testing "datetime handles date arguments intuitively and without being affected by the specific time now" (is (= "today" - (datetime (jt.ld/now))) + (datetime ld-now)) "LocalDate/now is today") + (is (= "today" + (datetime ld-now + :now-dt (jt.ldt/now))) + "has no problems with ldt :now-dt arguments") (is (and (= "in 1 day" (datetime (jt.ld/plus-days ld-now 1))) (= "1 day ago"