marnusw / date-fns-tz

Complementary library for date-fns v2 adding IANA time zone support
MIT License
1.06k stars 116 forks source link

`formatInTimeZone(d, tz)` shows the wrong time for one hour if the system zone has DST, even though `tz` doesn’t #258

Open andersk opened 10 months ago

andersk commented 10 months ago

My system time zone is America/Los_Angeles, where the start of daylight saving time on 2023-03-12 jumped the local time directly from 01:59 (-08:00) to 03:00 (-07:00). The UTC time zone had no such jump, of course, so one wouldn’t expect any issues formatting a UTC date between 01:59 (Z) and 03:00 (Z) for display in UTC.

However, because formatInTimeZone works internally by converting the time in the given zone to a “corresponding” local time in the system zone, this doesn’t work.

> formatInTimeZone(new Date('2023-03-12T01:05Z'), 'UTC', 'Pp z')
'03/12/2023, 1:05 AM UTC'
> formatInTimeZone(new Date('2023-03-12T02:05Z'), 'UTC', 'Pp z')  // expected 2:05 AM UTC
'03/12/2023, 3:05 AM UTC'
> formatInTimeZone(new Date('2023-03-12T03:05Z'), 'UTC', 'Pp z')
'03/12/2023, 3:05 AM UTC'
> formatInTimeZone(new Date('2023-03-12T04:05Z'), 'UTC', 'Pp z')
'03/12/2023, 4:05 AM UTC'

(Using date-fns-tz@2.0.0 on Node.js 20.9.0 with America/Los_Angeles as the system time zone.)