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 inconsistent with different DST moments #37

Open
cbejensen opened this issue Jan 9, 2025 · 1 comment
Open

TZDate inconsistent with different DST moments #37

cbejensen opened this issue Jan 9, 2025 · 1 comment

Comments

@cbejensen
Copy link

cbejensen commented Jan 9, 2025

I'm not sure why these produce different results. Both fall on a "spring forward" moment. In the first case, date-fns jumps forward as expected, but in the latter it seems to go backward to a different moment in time.

new TZDate('2012-03-11 02:00:00', 'America/New_York').toString();
//         "2012-03-11 03:00:00 -04:00" 👍🏼 

new TZDate('spring forward Asia', '2019-03-29 00:00:00', 'Asia/Amman').toString();
//                                "2019-03-28 11:00:00 +02:00" 👎🏼 

https://stackblitz.com/edit/js-33ru1iv7?devToolsHeight=33&file=index.js
(note that I'm comparing with moment-timezone in that StackBlitz just for clarity, but that's not necessary; it seems like date-fns is inconsistent with itself)

@cbejensen cbejensen changed the title TZDate inconsistent with different signatures and DST moments TZDate inconsistent with different DST moments Jan 10, 2025
@cbejensen
Copy link
Author

cbejensen commented Jan 10, 2025

Did some more testing, and it almost seems like, when given an invalid date of a DST switch, it falls back to an hour before unless it's in the USA, in which case it jumps to the next valid hour in DST. That doesn't sound right, it's just what 'm seeing. Here are a bunch of examples

View Code
// For each time zone, log the hour before the spring forward moment, then the moment itself, then the hour after

// Represented as the hour AFTER the switch
console.log('America/Los_Angeles');
console.log(new TZDate(2022, 2, 13, 1, 'America/Los_Angeles').toString()); 
// Sun Mar 13 2022 01:00:00 GMT-0800 (Pacific Standard Time)
console.log(new TZDate(2022, 2, 13, 2, 'America/Los_Angeles').toString()); 
// Sun Mar 13 2022 03:00:00 GMT-0700 (Pacific Daylight Time)
console.log(new TZDate(2022, 2, 13, 3, 'America/Los_Angeles').toString()); 
// Sun Mar 13 2022 03:00:00 GMT-0700 (Pacific Daylight Time)

// Represented as the hour before the switch
console.log('America/Mexico_City');
console.log(new TZDate(2014, 3, 6, 1, 'America/Mexico_City').toString()); 
// Sun Apr 06 2014 01:00:00 GMT-0600 (Central Standard Time)
console.log(new TZDate(2014, 3, 6, 2, 'America/Mexico_City').toString()); 
// Sun Apr 06 2014 01:00:00 GMT-0600 (Central Standard Time)
console.log(new TZDate(2014, 3, 6, 3, 'America/Mexico_City').toString()); 
// Sun Apr 06 2014 03:00:00 GMT-0500 (Central Daylight Time)

// Represented as the hour AFTER the switch
console.log('America/New_York');
console.log(new TZDate(2012, 2, 11, 1, 'America/New_York').toString()); 
// Sun Mar 11 2012 01:00:00 GMT-0500 (Eastern Standard Time)
console.log(new TZDate(2012, 2, 11, 2, 'America/New_York').toString()); 
// Sun Mar 11 2012 03:00:00 GMT-0400 (Eastern Daylight Time)
console.log(new TZDate(2012, 2, 11, 3, 'America/New_York').toString()); 
// Sun Mar 11 2012 03:00:00 GMT-0400 (Eastern Daylight Time)

// Represented as the hour before the switch
console.log('America/Barbados');
console.log(new TZDate(1980, 3, 20, 1, 'America/Barbados').toString()); 
// Sun Apr 20 1980 01:00:00 GMT-0400 (Atlantic Standard Time)
console.log(new TZDate(1980, 3, 20, 2, 'America/Barbados').toString()); 
// Sun Apr 20 1980 01:00:00 GMT-0400 (Atlantic Standard Time)
console.log(new TZDate(1980, 3, 20, 3, 'America/Barbados').toString()); 
// Sun Apr 20 1980 03:00:00 GMT-0300 (Atlantic Daylight Time)

// Represented as the hour before the switch
console.log('America/Sao_Paulo');
console.log(new TZDate(2018, 10, 3, 23).toString()); 
// Sat Nov 03 2018 23:00:00 GMT-0600 (Mountain Daylight Time)
console.log(new TZDate(2018, 10, 4, 'America/Sao_Paulo').toString()); 
// Sat Nov 03 2018 23:00:00 GMT-0300 (Brasilia Standard Time)
console.log(new TZDate(2018, 10, 4, 1, 'America/Sao_Paulo').toString()); 
// Sun Nov 04 2018 01:00:00 GMT-0200 (Brasilia Summer Time)

// Represented as the hour before the switch
console.log('Atlantic/Azores');
console.log(new TZDate(2024, 2, 30, 23, 'Atlantic/Azores').toString()); 
// Sat Mar 30 2024 23:00:00 GMT-0100 (Azores Standard Time)
console.log(new TZDate(2024, 2, 31, 'Atlantic/Azores').toString()); 
// Sat Mar 30 2024 23:00:00 GMT-0100 (Azores Standard Time)
console.log(new TZDate(2024, 2, 31, 1, 'Atlantic/Azores').toString()); 
// Sun Mar 31 2024 01:00:00 GMT+0000 (Azores Summer Time)

// Represented as the hour before the switch
console.log('Asia/Amman');
console.log(new TZDate(2019, 2, 28, 23, 'Asia/Amman').toString()); 
// Thu Mar 28 2019 23:00:00 GMT+0200 (Eastern European Standard Time)
console.log(new TZDate(2019, 2, 29, 0, 0, 0, 'Asia/Amman').toString()); 
// Thu Mar 28 2019 23:00:00 GMT+0200 (Eastern European Standard Time)
console.log(new TZDate(2019, 2, 29, 1, 'Asia/Amman').toString()); 
// Fri Mar 29 2019 01:00:00 GMT+0300 (Eastern European Summer Time)

// Represented as the hour before the switch
console.log('Europe/Moscow');
console.log(new TZDate(2010, 2, 28, 1, 'Europe/Moscow').toString()); 
// Sun Mar 28 2010 01:00:00 GMT+0300 (Moscow Standard Time)
console.log(new TZDate(2010, 2, 28, 2, 'Europe/Moscow').toString()); 
// Sun Mar 28 2010 01:00:00 GMT+0300 (Moscow Standard Time)
console.log(new TZDate(2010, 2, 28, 3, 'Europe/Moscow').toString()); 
// Sun Mar 28 2010 03:00:00 GMT+0400 (Moscow Summer 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