andrewmcveigh / cljs-time

A clj-time inspired date library for clojurescript.
342 stars 57 forks source link

Calculating time intervals under daylight savings time? #133

Open danielcompton opened 5 years ago

danielcompton commented 5 years ago

In New Zealand, the following test will fail:

(let [date-start (format/parse-local-date yyyy-mm-dd "2016-09-01")
          date-end   (format/parse-local-date yyyy-mm-dd "2016-09-30")]
      (let [interval (time/interval date-start date-end)]
        (is (= 29 (time/in-days interval))))
Chrome 71.0.3578 (Mac OS X 10.14.1) day8.apps-lib.cljstime-local-date-test local-date-interval-test FAILED
    FAIL in   (local-date-interval-test) (cljs/test.js:433:14)
    expected: (=
                29
                (time/in-days interval))
      actual: (=
                29
                28)
        diff: - 29
              + 28

The issue is that in 2016, New Zealand switched over to daylight savings time on Sunday 25th September. (time/in-days) on Interval bottoms out by calculating the millisecond difference between two dates. On Sunday 25th the clocks went forward an hour, meaning there was 'one less hour' in that day. This means that there is one hour less between the two local dates than there would ordinarily be at other times of the year.

I wonder whether in-days on at least goog.date.Date should count the number of days between the dates, not counting the number of milliseconds between them?

henryw374 commented 5 years ago

@danielcompton I had some similar issues a while back with local dates.

I did a couple of PR's to fix but decided to work on a new library, which has resulted in tick https://github.com/juxt/tick . We're planning to do an announce shortly.

danielcompton commented 5 years ago

Thanks, that was helpful in pointing me towards this comment: https://github.com/andrewmcveigh/cljs-time/issues/22#issuecomment-208514810. I've been experimenting locally with making a UtcDate type where DateTime : UtcDateTime :: Date : UtcDate. It seems to provide a good foundation for dealing with local dates that have no time nor timezone.