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

Inconsistent Date Handling for Pre-1883 Dates in TZDate #46

Open
GianlucaWassermeyer opened this issue Mar 3, 2025 · 0 comments
Open

Comments

@GianlucaWassermeyer
Copy link

GianlucaWassermeyer commented Mar 3, 2025

I encountered an issue with the TZDate class when handling dates before the implementation of the GMT system in 1883. The TZDate class appears to only consider the hour and minute of the timezone, but not the seconds, leading to inconsistencies.

Steps to Reproduce:

Create a TZDate object with a date before 1883.
Compare the output of ISOString, toString, and getDate methods with the same methods of a pure JavaScript Date object.
Example Code:

import { TZDate, TZDateMini } from '@date-fns/tz';
import { lastDayOfMonth, startOfMonth } from 'date-fns';

const tzDate = new TZDate('1860-01-01T00:00:57.000Z', 'America/Los_Angeles');
const jsDate = new Date('1860-01-01T00:00:57.000Z');

console.log('TZDate');
console.log('ISOString:', tzDate.toISOString());
console.log('toString:', tzDate.toString());
console.log('getDate:', tzDate.getDate());

console.log('pure javascript date');
console.log('ISOString:', jsDate.toISOString());
console.log('toString:', jsDate.toString());
console.log('getDate:', jsDate.getDate());

console.log('Error in use');
console.log('Last day of month (TZDate):', lastDayOfMonth(tzDate).toString());
console.log('Last day of month (Date):', lastDayOfMonth(jsDate).toString());

Observed Output:

TZDate
ISOString: 1860-01-01T00:00:57.000-07:52
toString: Sun Jan 01 1860 00:00:57 GMT-0752 (GMT-07:52:58)
getDate: 1

pure javascript date
ISOString: 1860-01-01T07:52:57.000Z
toString: Sat Dec 31 1859 23:59:59 GMT-0752 (Pacific Standard Time)
getDate: 31

Error in use
Last day of month (TZDate): Mon Jan 30 1860 23:59:00 GMT-0752 (Pacific Standard Time)
Last day of month (Date): Sat Dec 31 1859 00:00:00 GMT-0752 (Pacific Standard Time)

Expected Output: The TZDate and pure JavaScript Date objects should produce consistent results for the same date and timezone.

Example

Additional Information: The issue seems to stem from TZDate only considering the hour and minute of the timezone, but not the seconds. This discrepancy becomes apparent for dates before 1883, when the GMT system was implemented. The offset GMT-07:52:58 (America/Los_Angelos, GMT-08:00) causes the inconsistency.

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