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

use ISO string when converting system zone to JS date #1509

Closed
wants to merge 1 commit into from

Conversation

icambron
Copy link
Member

I'm of two minds about whether this is a good idea or not. This is to fix issues where historic zones have offsets that aren't whole minutes. Examples:

I'm sure there are others. Anyway, if it's a SystemZone date, we are using getTimezoneOffset (which provides whole minutes) to compute the epoch milliseconds from the input string, which will be off by however many seconds in the offset. When we convert we convert that to a JS date (for example, to format it), it will then be the wrong date. This fixes that by providing the ISO string directly to the JS Date, which will parse it using the more accurate offset the native date has access to internally.

The problem with the fix is that it may lead to other inconsistencies. The epoch millis in the DateTime will be different from the epoch millis in the native date. It's not clear to me whether this is making things better or merely making them weirder.

@diesieben07
Copy link
Collaborator

IANAZone#offset already does weird things to get the offset: It creates a DateTimeFormat with its timezone and then does trickery to extract the offset. This works fine (although we should probably not use floating point fractions here due to precision):

const ts = -5364644638000; // 1800-01-01T00:00:00.0000000 in America/New_York
console.log(IANAZone.create("America/New_York").offset(ts)); // outputs -296.03333333333336

We can use the same trickery in SystemZone, except not give a zone name to Intl.DateTimeFormat, so it uses the system zone.
The only issue with this is that either we have to recreate the DTF every time or cache it, in which case it would report the wrong value when the system's zone changes. But I think we have that same issue in other places already.
Additionally we might be able to find a way to know whether we can get away with just calling Date#getTimezoneOffset vs. going the Intl route.

@icambron
Copy link
Member Author

Yeah, the IANA zone handles this just fine (though we should probably change the offset interface to return seconds). I have been loathe to port this logic to the system zone just because every Intl operation is so slow. The vast majority of real world uses are not on historic time zones, so this would be a poor tradeoff.

Yes, perhaps a heuristic on when to use which would work. Old system datetimes would be slower and new ones would be fast. We'd just have to pick the cutoff carefully.

@diesieben07
Copy link
Collaborator

For the system zone we can get away without using Intl, because Date uses the system zone:

const date = new Date(-5364644638000);
const fakeUTC = Date.UTC(
  date.getFullYear(), date.getMonth(), date.getDate(),
  date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()
);
const offset = (date.valueOf() - fakeUTC) / 1000;
console.log(date.getTimezoneOffset() * 60, offset); // 17760, 17762

@icambron
Copy link
Member Author

That's a good point

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

Successfully merging this pull request may close these issues.

2 participants