date-fns-tz icon indicating copy to clipboard operation
date-fns-tz copied to clipboard

Handling of zonedTimeToUtc when zoned time is during daylight saving jump forward is 1 hour too early

Open mcshaz opened this issue 2 years ago • 1 comments

if you look at the diff for pull request #220 (tests only, all currently passing), you can see the current behaviour of zonedTimeToUtc when the time is set to a zoned time which doesn't exist (because the clock jumps forward and skips that hour)

 it('handles times that dont exist with clock jump forward: -ve UTC offset', function () {
      // at 02:00 local clock will immediately jump forward to 03:00
      var result = zonedTimeToUtc('2023-03-12T02:00:00', 'America/New_York')
      // UTC 06:59 is NY 01:59 GMT -5
      // UTC 07:00 is NY 03:00 GMT -4
      // !! this is an error - should either throw error OR return UTC 07:00
      // it should NOT return 06:00 but defining problem before making tests fail
      assert.deepEqual(result.toISOString(), '2023-03-12T06:00:00.000Z')
 })

as per the comments in the above test - I believe if set to 02:00, returning the UTC time which corresponds to 1 hour prior to moving the clock forward is a mistake, and should either return UTC @ the moment the clock had ticked over from 01:59 or throw an error.

mcshaz avatar Jan 12 '23 01:01 mcshaz

I have a workaround written in typescript in another library. Putting the link here in case someone is having a similar problem. https://github.com/mcshaz/caldate/blob/named-exports/src/zonedTimeToUtc.ts

mcshaz avatar Jan 16 '23 21:01 mcshaz