Skip to content

Commit

Permalink
truncate works for all truncatables fixes juxt#145
Browse files Browse the repository at this point in the history
  • Loading branch information
henryw374 committed Oct 28, 2021
1 parent 02452f3 commit 91d0d18
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ install:
clojure -M:release install --version $(VERSION)
deploy:
clojure -M:release --version $(VERSION)
lint:
clj-kondo --lint src test

# hooray for stackoverflow
.PHONY: list
Expand Down
19 changes: 18 additions & 1 deletion src/tick/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -535,9 +535,26 @@
:when k]
[k (cljc.java-time.temporal.temporal-amount/get x tu)])))

(extend-protocol p/ITruncate
Instant
(truncate [x u ]
(cljc.java-time.instant/truncated-to x (get unit-map u)))
LocalDateTime
(truncate [x u ]
(cljc.java-time.local-date-time/truncated-to x (get unit-map u)))
ZonedDateTime
(truncate [x u ]
(cljc.java-time.zoned-date-time/truncated-to x (get unit-map u)))
OffsetDateTime
(truncate [x u ]
(cljc.java-time.offset-date-time/truncated-to x (get unit-map u)))
LocalTime
(truncate [x u ]
(cljc.java-time.local-time/truncated-to x (get unit-map u))))

(defn truncate [x u]
{:pre [(contains? unit-map u)]}
(cljc.java-time.instant/truncated-to x (get unit-map u)))
(p/truncate x u))

;; Durations & Periods

Expand Down
3 changes: 3 additions & 0 deletions src/tick/protocols.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
(ns tick.protocols
(:refer-clojure :exclude [+ - inc dec max min range time int long = < <= > >= next >> << atom swap! swap-vals! compare-and-set! reset! reset-vals! second divide]))

(defprotocol ITruncate
(truncate [date-time unit-kw]))

(defprotocol ITimeReify
(on [time date] "Set time be ON a date")
(at [date time] "Set date to be AT a time")
Expand Down
15 changes: 15 additions & 0 deletions test/tick/api_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@
(t/zoned-date-time i)
(t/offset-date-time i)])

(deftest truncate-test
(let [dates [(t/instant) (t/zoned-date-time) (t/date-time)
(t/offset-date-time) (t/time)]
truncate-tos [:nanos
:micros
:millis
:seconds
:minutes
:hours
:half-days
:days ]]
(doseq [date dates
truncate-to truncate-tos]
(is (t/truncate date truncate-to)))))

(deftest comparison-test
(let [point (t/truncate (t/instant) :millis)
later (t/>> point (t/new-duration 1 :millis))]
Expand Down

0 comments on commit 91d0d18

Please sign in to comment.