dm3/clojure.java-time

unexpected results for before? after? on interval

thumbnail opened this issue · 3 comments

According to the threethen javadoc an interval is start inclusive and the end exclusive..

The javadoc of the isBefore and isAfter reflect this:

Since intervals do not include their end points, this will return true if the instant equals the end of the interval.

Using java-time however, this is not the case:

i
=> #object[org.threeten.extra.Interval 0x17e871bf "2018-01-01T00:00:00Z/2019-01-01T00:00:00Z"]
i2
=> #object[org.threeten.extra.Interval 0x51897443 "2019-01-01T00:00:00Z/2020-01-01T00:00:00Z"]
(.isBefore i i2)
=> true
(java-time/before? i i2)
=> false

The issue is a custom impl for single-before? and single-after?:

jt.c/Ordered
(single-before? [i o] (if (jt.t/instant? o)
(.isBefore (.getEnd i) o)
(.isBefore (.getEnd i) (.getStart ^Interval o))))
(single-after? [i o] (if (jt.t/instant? o)
(.isAfter (.getStart i) o)
(.isAfter (.getStart i) (.getEnd ^Interval o))))

and can be mitigated by evalling

(extend-type Interval
  jt.c/Ordered
  (single-before? [i o] (.isBefore i o))
  (single-after? [i o] (.isAfter i o)))
dm3 commented

This is a problem. I'm not sure why I did the single-before?/after? on the Interval this way. Seems like delegating to the .isBefore/After should be the right choice. Will look into it.

dm3 commented

I've just looked briefly - looks like the single-before?/after? were defined before there was an .isBefore/After implementation in Interval added in v1.0.