From 027db4ac4757830cc5e83cc966d3b5cfbb50b41c Mon Sep 17 00:00:00 2001 From: Jeffrey 'jf' Lim Date: Sat, 16 Nov 2024 05:24:39 +0800 Subject: [PATCH] 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))))