Skip to content

t/micros returns wrong result for negative durations #208

@yuhan0

Description

@yuhan0

Observed behaviour

(require '[tick.core :as t])

(t/of-seconds -1)            ;=> #time/duration "PT-1S"
(t/nanos  (t/of-seconds -1)) ;=> -1000000000   ✓
(t/millis (t/of-seconds -1)) ;=> -1000          ✓
(t/micros (t/of-seconds -1)) ;=> 18446744072709551  ✗

(t/micros (t/of-micros -42)) ;=> 18446744073709509  ✗

Note: The result 18446744072709551 is -1e9 reinterpreted as an unsigned 64-bit integer and then divided.

Root cause

micros is implemented as:

(micros [d] (#?(:clj Long/divideUnsigned :cljs cljs.core//) (p/nanos d) 1000))

This bug has been present from its initial implementation in a5f1703

It's unclear why Long/divideUnsigned was used in the first place - presumably to handle positive durations whose nanosecond count exceeds Long/MAX_VALUE (~ 292 years)? But this silently corrupts any negative duration(which are legitimate and supported by the spec) , producing a large positive number with no indication of error.

Suggested fix

(micros [d]
  (clojure.core/+ (* (cljc.java-time.duration/get-seconds d) 1000000)
                  (quot (cljc.java-time.duration/get-nano d) 1000)))

Incidentally this also fixes another bug in the current impl, where the cljs branch returns a non-truncated value:

(t/micros (t/of-nanos 1234)) ;=> 1     (JVM Clojure) 
                             ;=> 1.234 (ClojureScript) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions