Search code examples
clojurejava-time

take-while and java-time/iterate creating odd results


I'm using clojure.java-time version 0.3.2 and I have a TemporalAdjuster defined like this:

(def next-pay-day
  (reify java.time.temporal.TemporalAdjuster
    (adjustInto [this temporal]
      (let [local-date (java.time.LocalDate/from temporal)
            day-of-month (j/as local-date :day-of-month)
            last-day-of-month (j/as (j/adjust local-date :last-day-of-month) :day-of-month)]
        (cond 
          (= day-of-month last-day-of-month) 
          (j/plus local-date (j/days 14))

          (< day-of-month 14) 
          (j/plus local-date (j/days (- 14 day-of-month)))

          :else 
          (j/adjust local-date :last-day-of-month))))))

Which works something like this:

=> (require '[pto-calculator.logic.pay-periods :as p])
nil
=> (require '[java-time :as j])
nil
=> (take 4 (j/iterate j/adjust (j/local-date 2019 3 3) p/next-pay-day))
(#object[java.time.LocalDate 0x74e53020 "2019-03-03"] #object[java.time.LocalDate 0x49980a "2019-03-14"] #object[java.time.LocalDate 0x2e332a36 "2019-03-31"] #object[java.time.LocalDate 0x5e7aaec6 "2019-04-14"])

I'm trying to generate a list of pay days between today and t, where t is some LocalDate in the future. I have this, but the problem is it seems to work for some dates (April 15th):

=> (take-while #(= 1 (compare (j/local-date 2019 4 15) %)) (j/iterate j/adjust (j/local-date 2019 3 3) p/next-pay-period))
(#object[java.time.LocalDate 0xc994eb6 "2019-03-03"] #object[java.time.LocalDate 0x3ec8e48 "2019-03-14"] #object[java.time.LocalDate 0x1bddd9d5 "2019-03-31"] #object[java.time.LocalDate 0x133ed931 "2019-04-14"])
; Last date is 2019-04-14 (expected)

But not for others (April 18th):

=> (take-while #(= 1 (compare (j/local-date 2019 4 18) %)) (j/iterate j/adjust (j/local-date 2019 3 3) p/next-pay-period))
(#object[java.time.LocalDate 0x7f0ac64b "2019-03-03"] #object[java.time.LocalDate 0x1f1d3684 "2019-03-14"] #object[java.time.LocalDate 0x70415bc3 "2019-03-31"])
; Last date is 2019-03-31 (unexpected, should be 2019-04-14)

Why is the operation cutting short for April 18th, but working fine for April 15th?


Solution

  • compare does not always return 1, 0, or -1. So, instead of:

    #(= 1 (compare (j/local-date 2019 4 15) %))
    

    try:

    #(pos? (compare (j/local-date 2019 4 15) %))