Skip to content

Commit 0e327b5

Browse files
authored
Merge pull request #45 from jf/fix_datetime_calc_for_dates
fix reported difference in datetime when passed in a date in the future (addresses #44)
2 parents db05a6e + 85ef9c1 commit 0e327b5

3 files changed

Lines changed: 39 additions & 4 deletions

File tree

src/clj_commons/humanize.cljc

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[clj-commons.humanize.inflect :refer [pluralize-noun in?]]
55
[clj-commons.humanize.time-convert :refer [coerce-to-local-date-time]]
66
[cljc.java-time.duration :as jt.duration]
7+
[cljc.java-time.extn.predicates :as jt.predicates]
78
[cljc.java-time.local-date-time :as jt.ldt]
89
[clojure.string :as string :refer [join]]
910
#?@(:cljs [[goog.string :as gstring]
@@ -292,8 +293,16 @@
292293
:or {now-dt (jt.ldt/now)
293294
suffix "ago"
294295
prefix "in"}}]
295-
(let [then-dt (coerce-to-local-date-time then-dt)
296+
(let [local-date? (jt.predicates/local-date? then-dt)
297+
then-dt (coerce-to-local-date-time then-dt)
296298
now-dt (coerce-to-local-date-time now-dt)
299+
now-dt (if local-date?
300+
(-> now-dt
301+
(jt.ldt/with-hour 0)
302+
(jt.ldt/with-minute 0)
303+
(jt.ldt/with-second 0)
304+
(jt.ldt/with-nano 0))
305+
now-dt)
297306
future-time? (jt.ldt/is-after then-dt now-dt)
298307
;; get the Duration between the two times
299308
time-between (-> (jt.duration/between then-dt now-dt)
@@ -342,6 +351,9 @@
342351
future-time?
343352
(str prefix " a moment")
344353

354+
local-date?
355+
"today"
356+
345357
:else
346358
(str "a moment " suffix))))
347359

src/clj_commons/humanize/time_convert.cljc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"Internal utility to convert strings and other typs into LocalDateTime "
44
(:require [cljc.java-time.extn.predicates :as jt.predicates]
55
[cljc.java-time.format.date-time-formatter :as dt.formats]
6+
[cljc.java-time.local-date :as jt.ld]
67
[cljc.java-time.local-date-time :as jt.ldt]
78
[cljc.java-time.instant :as jt.i]
89
[cljc.java-time.zone-id :as jt.zi]
@@ -38,7 +39,10 @@
3839
(jvm/java-util-date? t)
3940
(jt.ldt/parse (jvm/java-util-date->iso8601-str t) dt.formats/iso-date-time)]
4041
:cljs [(instance? js/Date t)
41-
(jt.ldt/of-instant (jt.i/of-epoch-milli (.getTime t)) (jt.zi/system-default))])
42+
(jt.ldt/of-instant (jt.i/of-epoch-milli (.getTime t)) (jt.zi/system-default))
43+
44+
(jt.predicates/local-date? t)
45+
(jt.ld/at-start-of-day t)])
4246

4347
;; Strings
4448
(looks-like-an-iso8601-string? t)

test/clj_commons/humanize_test.cljc

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
duration]
88
:as h]
99
[clojure.math :as math]
10+
[cljc.java-time.local-date :as jt.ld]
1011
[clj-commons.humanize.time-convert :refer [coerce-to-local-date-time]]
1112
[cljc.java-time.local-date-time :as jt.ldt]))
1213

@@ -218,7 +219,8 @@
218219

219220
(deftest datetime-test
220221
(let [t1-str "2022-01-01T01:00:00"
221-
t1 (jt.ldt/parse t1-str)]
222+
t1 (jt.ldt/parse t1-str)
223+
ld-now (jt.ld/now)]
222224
(is (= "a moment ago"
223225
(datetime (jt.ldt/now)))
224226
":now-dt is optional")
@@ -258,7 +260,24 @@
258260
(datetime (jt.ldt/plus-years t1 1)
259261
:now-dt t1
260262
:suffix "foo"))
261-
"suffix for a time in the past does nothing"))))
263+
"suffix for a time in the past does nothing"))
264+
(testing "datetime handles date arguments intuitively and without being affected by the specific time now"
265+
(is (= "today"
266+
(datetime ld-now))
267+
"today's date returns \"today\"")
268+
(is (= "today"
269+
(datetime ld-now
270+
:now-dt (jt.ldt/now)))
271+
"has no problems with ldt :now-dt arguments")
272+
(is (and (= "in 1 day"
273+
(datetime (jt.ld/plus-days ld-now 1)))
274+
(= "1 day ago"
275+
(datetime (jt.ld/minus-days ld-now 1)))
276+
(= "in 3 days"
277+
(datetime (jt.ld/plus-days ld-now 3)))
278+
(= "3 days ago"
279+
(datetime (jt.ld/minus-days ld-now 3))))
280+
"equidistant dates in the future vs past are accounted for in the same way"))))
262281

263282
(deftest durations
264283
(testing "duration to terms"

0 commit comments

Comments
 (0)