Skip to content

Commit

Permalink
Polyfill: Implement proposal-canonical-tz Stage 3
Browse files Browse the repository at this point in the history
  • Loading branch information
justingrant committed Jul 22, 2023
1 parent 8911181 commit ce25d04
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
1 change: 1 addition & 0 deletions polyfill/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,7 @@ export namespace Temporal {
static from(timeZone: TimeZoneLike): Temporal.TimeZone | TimeZoneProtocol;
constructor(timeZoneIdentifier: string);
readonly id: string;
equals(timeZone: TimeZoneLike): boolean;
getOffsetNanosecondsFor(instant: Temporal.Instant | string): number;
getOffsetStringFor(instant: Temporal.Instant | string): string;
getPlainDateTimeFor(instant: Temporal.Instant | string, calendar?: CalendarLike): Temporal.PlainDateTime;
Expand Down
20 changes: 18 additions & 2 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2132,7 +2132,7 @@ export function ToTemporalTimeZoneSlotValue(temporalTimeZoneLike) {

const record = GetAvailableNamedTimeZoneIdentifier(tzName);
if (!record) throw new RangeError(`Unrecognized time zone ${tzName}`);
return record.primaryIdentifier;
return record.identifier;
}
if (z) return 'UTC';
// if !tzName && !z then offset must be present
Expand Down Expand Up @@ -2160,7 +2160,23 @@ export function TimeZoneEquals(one, two) {
if (one === two) return true;
const tz1 = ToTemporalTimeZoneIdentifier(one);
const tz2 = ToTemporalTimeZoneIdentifier(two);
return tz1 === tz2;
if (tz1 === tz2) return true;
const offsetMinutes1 = ParseTimeZoneIdentifier(tz1).offsetMinutes;
const offsetMinutes2 = ParseTimeZoneIdentifier(tz2).offsetMinutes;
if (offsetMinutes1 === undefined && offsetMinutes2 === undefined) {
// Calling GetAvailableNamedTimeZoneIdentifier is costly, so (unlike the
// spec) the polyfill will early-return if one of them isn't recognized. Try
// the second ID first because it's more likely to be unknown, because it
// can come from the argument of TimeZone.p.equals as opposed to the first
// ID which comes from the receiver.
const idRecord2 = GetAvailableNamedTimeZoneIdentifier(tz2);
if (!idRecord2) return false;
const idRecord1 = GetAvailableNamedTimeZoneIdentifier(tz1);
if (!idRecord1) return false;
return idRecord1.primaryIdentifier === idRecord2.primaryIdentifier;
} else {
return offsetMinutes1 === offsetMinutes2;
}
}

export function TemporalDateTimeToDate(dateTime) {
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/intl.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ Object.defineProperty(DateTimeFormat, 'prototype', {

function resolvedOptions() {
const resolved = this[ORIGINAL].resolvedOptions();
resolved.timeZone = this[TZ_CANONICAL];
resolved.timeZone = this[TZ_ORIGINAL];
return resolved;
}

Expand Down
7 changes: 6 additions & 1 deletion polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class TimeZone {
} else {
const record = ES.GetAvailableNamedTimeZoneIdentifier(stringIdentifier);
if (!record) throw new RangeError(`Invalid time zone identifier: ${stringIdentifier}`);
stringIdentifier = record.primaryIdentifier;
stringIdentifier = record.identifier;
}
CreateSlots(this);
SetSlot(this, TIMEZONE_ID, stringIdentifier);
Expand All @@ -46,6 +46,11 @@ export class TimeZone {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
return GetSlot(this, TIMEZONE_ID);
}
equals(other) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
const timeZoneSlotValue = ES.ToTemporalTimeZoneSlotValue(other);
return ES.TimeZoneEquals(this, timeZoneSlotValue);
}
getOffsetNanosecondsFor(instant) {
if (!ES.IsTemporalTimeZone(this)) throw new TypeError('invalid receiver');
instant = ES.ToTemporalInstant(instant);
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export class ZonedDateTime {
} else {
const record = ES.GetAvailableNamedTimeZoneIdentifier(timeZoneIdentifier);
if (!record) throw new RangeError(`toLocaleString formats built-in time zones, not ${timeZoneIdentifier}`);
optionsCopy.timeZone = record.primaryIdentifier;
optionsCopy.timeZone = record.identifier;
}

const formatter = new DateTimeFormat(locales, optionsCopy);
Expand Down

0 comments on commit ce25d04

Please sign in to comment.