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)))) diff --git a/test/clj_commons/humanize_test.cljc b/test/clj_commons/humanize_test.cljc index 8346528..2d1c6f0 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,24 @@ (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 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" + (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"