Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TZDate fall back depends on local time #40

Open
cbejensen opened this issue Jan 15, 2025 · 0 comments
Open

TZDate fall back depends on local time #40

cbejensen opened this issue Jan 15, 2025 · 0 comments

Comments

@cbejensen
Copy link

cbejensen commented Jan 15, 2025

It seems like TZDate(year, month, day, hour, timeZone) returns the date as it occurs in the given timezone. That's great, and intuitive! However, if the date passed is on the hour that is repeated during a "fall back" transition from Daylight Saving Time to Standard Time, it returns different moments in time based on my local time zone, which shouldn't be related.

From what I can tell, if the provided time zone is the same as my local system timezone or earlier, it uses the lesser offset. For example:

// when local/system time is Los Angeles, where 1am could be offset -7 or -8
console.log(new TZDate(2012, 10, 4, 1, 'America/Los_Angeles').toString()); // offset of -7
console.log(new TZDate(2012, 10, 4, 1, 'America/Denver').toString()); // offset of -7

// when local/system time is Denver, where 1am could be offset -6 or -7
console.log(new TZDate(2012, 10, 4, 1, 'America/Los_Angeles').toString()); // offset of -7
console.log(new TZDate(2012, 10, 4, 1, 'America/Denver').toString()); // offset of -6 ❗ 

More examples: https://stackblitz.com/edit/js-zbjsdksw?file=index.js

View Code
import { TZDate } from '@date-fns/tz';

// For each "fall back" moment, log the hour before, then the hour that's duplicated, then the hour after

console.log('Los Angeles');
console.log(new TZDate(2012, 10, 4, 'America/Los_Angeles').toString());
console.log(new TZDate(2012, 10, 4, 1, 'America/Los_Angeles').toString());
console.log(new TZDate(2012, 10, 4, 2, 'America/Los_Angeles').toString());

console.log('Denver');
console.log(new TZDate(2012, 10, 4, 'America/Denver').toString());
console.log(new TZDate(2012, 10, 4, 1, 'America/Denver').toString());
console.log(new TZDate(2012, 10, 4, 2, 'America/Denver').toString());

console.log('Chicago');
console.log(new TZDate(2012, 10, 4, 'America/Chicago').toString());
console.log(new TZDate(2012, 10, 4, 1, 'America/Chicago').toString());
console.log(new TZDate(2012, 10, 4, 2, 'America/Chicago').toString());

console.log('New York');
console.log(new TZDate(2012, 10, 4, 'America/New_York').toString());
console.log(new TZDate(2012, 10, 4, 1, 'America/New_York').toString());
console.log(new TZDate(2012, 10, 4, 2, 'America/New_York').toString());

/*
With system time zone in Los Angeles
Los Angeles
Sun Nov 04 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0800 (Pacific Standard Time)
Denver
Sun Nov 04 2012 00:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0700 (Mountain Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0700 (Mountain Standard Time)
Chicago
Sun Nov 04 2012 00:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0600 (Central Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0600 (Central Standard Time)
New York
Sun Nov 04 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0500 (Eastern Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0500 (Eastern Standard Time)
*/

/*
With system time zone in Denver
Los Angeles
Sun Nov 04 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0800 (Pacific Standard Time)
Denver
Sun Nov 04 2012 00:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0700 (Mountain Standard Time)
Chicago
Sun Nov 04 2012 00:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0600 (Central Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0600 (Central Standard Time)
New York
Sun Nov 04 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0500 (Eastern Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0500 (Eastern Standard Time)
*/

/*
With system time zone in Chicago
Los Angeles
Sun Nov 04 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0800 (Pacific Standard Time)
Denver
Sun Nov 04 2012 00:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0700 (Mountain Standard Time)
Chicago
Sun Nov 04 2012 00:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0600 (Central Standard Time)
New York
Sun Nov 04 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0500 (Eastern Standard Time)
Sun Nov 04 2012 02:00:00 GMT-0500 (Eastern Standard Time)
*/

/*
With system time zone in New York
Los Angeles
Sun Nov 04 2012 00:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0700 (Pacific Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0800 (Pacific Standard Time)
Denver
Sun Nov 04 2012 00:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0600 (Mountain Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0700 (Mountain Standard Time)
Chicago
Sun Nov 04 2012 00:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0500 (Central Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0600 (Central Standard Time)
New York
Sun Nov 04 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
Sun Nov 04 2012 01:00:00 GMT-0400 (Eastern Daylight Time)
Sun Nov 04 2012 02:00:00 GMT-0500 (Eastern Standard Time)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant