From 10036b95fc82b16603c48d3e132e38fffe3cdc64 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Sun, 3 May 2026 01:47:58 +0800 Subject: [PATCH 1/4] Fix tick.core/micros for negative durations (#208) Also fixes truncation bug in the :cljs branch --- src/tick/core.cljc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tick/core.cljc b/src/tick/core.cljc index 02b4491..5bc2d53 100644 --- a/src/tick/core.cljc +++ b/src/tick/core.cljc @@ -628,7 +628,7 @@ (extend-protocol p/ITimeLength Duration (nanos [d] (cljc.java-time.duration/to-nanos d)) - (micros [d] (#?(:clj Long/divideUnsigned :cljs cljs.core//) (p/nanos d) 1000)) + (micros [d] (quot (p/nanos d) 1000)) (millis [d] (cljc.java-time.duration/to-millis d)) (seconds [d] (cljc.java-time.duration/get-seconds d)) (minutes [d] (cljc.java-time.duration/to-minutes d)) From 5c120fb62f795aa26824e5e4101153cab56599fa Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Sun, 3 May 2026 02:09:33 +0800 Subject: [PATCH 2/4] Add tests for negative and truncated durations --- test/tick/api_test.cljc | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/test/tick/api_test.cljc b/test/tick/api_test.cljc index 9a781c3..25e4383 100644 --- a/test/tick/api_test.cljc +++ b/test/tick/api_test.cljc @@ -445,7 +445,39 @@ (is (= (t/of-millis 10) (t/new-duration 10 :millis))) (is (= (t/of-seconds 10) (t/new-duration 10 :seconds))) (is (= (t/of-minutes 10) (t/new-duration 10 :minutes))) - (is (= (t/of-hours 10) (t/new-duration 10 :hours)))) + (is (= (t/of-hours 10) (t/new-duration 10 :hours))) + + (testing "negative durations" + ;; #208: micros previously returned large positive values via Long/divideUnsigned + (is (= -1000000000 (t/nanos (t/of-seconds -1)))) + (is (= -1000000 (t/micros (t/of-seconds -1)))) + (is (= -1000 (t/millis (t/of-seconds -1)))) + (is (= -1 (t/seconds (t/of-seconds -1)))) + (is (= -1 (t/minutes (t/of-minutes -1)))) + (is (= -1 (t/hours (t/of-hours -1)))) + (is (= -1 (t/days (t/of-days -1))))) + + (testing "truncation" + ;; All unit fns truncate toward zero for positive durations + (is (= 1 (t/micros (t/of-nanos 1001)))) + (is (= 1 (t/millis (t/of-micros 1234)))) + (is (= 1 (t/seconds (t/of-millis 1999)))) + (is (= 1 (t/minutes (t/of-seconds 61)))) + (is (= 1 (t/hours (t/of-minutes 99)))) + (is (= 1 (t/days (t/of-hours 25)))) + + ;; Negative seconds truncate toward -Infinity (due to internal repr) + (is (= -2 (t/seconds (t/of-nanos -1000000001)))) + (is (= -2 (t/seconds (t/of-micros -1000001)))) + (is (= -2 (t/seconds (t/of-millis -1001)))) + + ;; All other negative units truncate toward zero + (is (= -1 (t/micros (t/of-nanos -1234)))) + (is (= -1 (t/millis (t/of-nanos -1999999)))) + (is (= -1 (t/millis (t/of-micros -1001)))) + (is (= -1 (t/minutes (t/of-seconds -61)))) + (is (= -1 (t/hours (t/of-minutes -99)))) + (is (= -1 (t/days (t/of-hours -25)))))) ;; Periods. Convenience functions to create periods of specific From 7a80248a62b115ba8845d185f96ec0d916e7d9e8 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Tue, 5 May 2026 21:28:01 +0800 Subject: [PATCH 3/4] Use deterministic clock for testing --- test/tick/api_test.cljc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/tick/api_test.cljc b/test/tick/api_test.cljc index 25e4383..8dabba6 100644 --- a/test/tick/api_test.cljc +++ b/test/tick/api_test.cljc @@ -34,9 +34,10 @@ (is (= "12:00" (str (t/noon)))))) (deftest date-construction-test - (is (= (t/date "2018-01-11") - (t/date (t/instant 1515691416624)))) - (is (t/date-time? (t/noon (t/today)))) + (t/with-clock (t/zone-offset 0) + (is (= (t/date "2018-01-11") + (t/date (t/instant 1515691416624)))) + (is (t/date-time? (t/noon (t/today))))) (t/with-clock (-> (t/date "2018-02-14") (t/at "10:00")) (testing "(noon (today))" (is (= "2018-02-14T12:00" (str (t/noon (t/today)))))) From 32b4280c2851799e25ab555142c2afc7121a1b85 Mon Sep 17 00:00:00 2001 From: yuhan0 Date: Tue, 5 May 2026 22:56:45 +0800 Subject: [PATCH 4/4] Wrap in reader cond, note platform discrepancy --- test/tick/api_test.cljc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/tick/api_test.cljc b/test/tick/api_test.cljc index 8dabba6..4f780eb 100644 --- a/test/tick/api_test.cljc +++ b/test/tick/api_test.cljc @@ -472,10 +472,12 @@ (is (= -2 (t/seconds (t/of-micros -1000001)))) (is (= -2 (t/seconds (t/of-millis -1001)))) + ;; Behavior of millis is platform-dependent (discrepancy in underlying js-joda lib vs java-time) + (is (= #?(:clj -1 :cljs -2) (t/millis (t/of-nanos -1999999)))) + (is (= #?(:clj -1 :cljs -2) (t/millis (t/of-micros -1001)))) + ;; All other negative units truncate toward zero (is (= -1 (t/micros (t/of-nanos -1234)))) - (is (= -1 (t/millis (t/of-nanos -1999999)))) - (is (= -1 (t/millis (t/of-micros -1001)))) (is (= -1 (t/minutes (t/of-seconds -61)))) (is (= -1 (t/hours (t/of-minutes -99)))) (is (= -1 (t/days (t/of-hours -25))))))