Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix reported difference in datetime when passed in a date in the future (addresses #44) #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/clj_commons/humanize.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -342,6 +351,9 @@
future-time?
(str prefix " a moment")

local-date?
"today"

:else
(str "a moment " suffix))))

Expand Down
23 changes: 21 additions & 2 deletions test/clj_commons/humanize_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down