Skip to content

Commit

Permalink
Fast-path conversion of Temporal.ZonedDateTime to Temporal.TimeZone
Browse files Browse the repository at this point in the history
If passing a Temporal.ZonedDateTime (that is, a Temporal object with a
[[TimeZone]] internal slot) where a Temporal.TimeZone is expected, then
read that slot instead of doing a Get on the "timeZone" property.

The Get operation was also not consistently done throughout the existing
spec text, so fix that as well. This means the TimeZoneFrom operation
becomes redundant, so remove it. Additionally, the check for the
"timeZone" property on plain objects was not implemented according to the
consensus in
#925 (comment)
and this change fixes that as well.

See: #1428
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent 07f6379 commit 3119ab8
Show file tree
Hide file tree
Showing 27 changed files with 53 additions and 34 deletions.
25 changes: 12 additions & 13 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,8 @@ export const ES = ObjectAssign({}, ES2020, {
calendar
} = ES.ParseTemporalZonedDateTimeString(ES.ToString(item)));
if (!ianaName) throw new RangeError('time zone ID required in brackets');
timeZone = ES.TimeZoneFrom(ianaName);
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
timeZone = new TemporalTimeZone(ianaName);
if (!calendar) calendar = ES.GetISO8601Calendar();
calendar = ES.ToTemporalCalendar(calendar);
}
Expand Down Expand Up @@ -1701,22 +1702,20 @@ export const ES = ObjectAssign({}, ES2020, {
if (!ES.IsTemporalMonthDay(result)) throw new TypeError('invalid result');
return result;
},
TimeZoneFrom: (item) => {
if (ES.Type(item) === 'Object') {
if (!('timeZone' in item)) return item;
item = item.timeZone;
if (ES.Type(item) === 'Object' && !('timeZone' in item)) return item;
}
const timeZone = ES.TemporalTimeZoneFromString(ES.ToString(item));
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
return new TemporalTimeZone(timeZone);
},

ToTemporalTimeZone: (temporalTimeZoneLike) => {
if (ES.Type(temporalTimeZoneLike) === 'Object') {
return temporalTimeZoneLike;
if (ES.IsTemporalZonedDateTime(temporalTimeZoneLike)) return GetSlot(temporalTimeZoneLike, TIME_ZONE);
if (!('timeZone' in temporalTimeZoneLike)) return temporalTimeZoneLike;
temporalTimeZoneLike = temporalTimeZoneLike.timeZone;
if (ES.Type(temporalTimeZoneLike) === 'Object' && !('timeZone' in temporalTimeZoneLike)) {
return temporalTimeZoneLike;
}
}
const identifier = ES.ToString(temporalTimeZoneLike);
return ES.TimeZoneFrom(identifier);
const timeZone = ES.TemporalTimeZoneFromString(identifier);
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
return new TemporalTimeZone(timeZone);
},
TimeZoneCompare: (one, two) => {
const tz1 = ES.ToString(one);
Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/timezone.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export class TimeZone {
return ES.ToString(this);
}
static from(item) {
return ES.TimeZoneFrom(item);
return ES.ToTemporalTimeZone(item);
}
}

Expand Down
1 change: 1 addition & 0 deletions polyfill/test/Instant/prototype/toString/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
"get timeZone.getOffsetNanosecondsFor",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = [];
const expected = [
"has timeZone.timeZone",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const dateTime = Temporal.PlainDateTime.from("1963-07-02T12:34:56.987654321");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = [];
const expected = [
"has timeZone.timeZone",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const dateTime = Temporal.PlainDateTime.from("1963-07-02T12:34:56.987654321");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = [];
const expected = [
"has timeZone.timeZone",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const dateTime = Temporal.PlainDateTime.from("1963-07-02T12:34:56.987654321");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ includes: [compareArray.js]
---*/

const actual = [];
const expected = [];
const expected = [
"has timeZone.timeZone",
];

const instant = Temporal.Instant.from("1975-02-02T14:25:36.123456789Z");
const dateTime = Temporal.PlainDateTime.from("1963-07-02T12:00:00.987654321");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get options.disambiguation",
"get disambiguation.toString",
"call disambiguation.toString",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/calendar-from-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/calendar-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/calendar-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/timezone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDate/toDate-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/calendar-from-undefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/calendar-function.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/calendar-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/timezone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainDateTime/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainTimeISO/timezone-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainTimeISO/timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
1 change: 1 addition & 0 deletions polyfill/test/now/plainTimeISO/toTime-override.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ includes: [compareArray.js]

const actual = [];
const expected = [
"has timeZone.timeZone",
"get timeZone.getOffsetNanosecondsFor",
"call timeZone.getOffsetNanosecondsFor",
];
Expand Down
23 changes: 8 additions & 15 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ <h1>Temporal.TimeZone.from ( _item_ )</h1>
The following steps are taken:
</p>
<emu-alg>
1. Return ? TimeZoneFrom(_item_).
1. Return ? ToTemporalTimeZone(_item_).
</emu-alg>
</emu-clause>
</emu-clause>
Expand Down Expand Up @@ -534,21 +534,14 @@ <h1>FormatTimeZoneOffsetString ( _offsetNanoseconds_ )</h1>
<emu-clause id="sec-temporal-totemporaltimezone" aoid="ToTemporalTimeZone">
<h1>ToTemporalTimeZone ( _temporalTimeZoneLike_ )</h1>
<emu-alg>
1. If Type(_temporalTimeZoneLike_) is Object, return _temporalTimeZoneLike_.
1. If Type(_temporalTimeZoneLike_) is Object, then
1. If _temporalTimeZoneLike_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Return _temporalTimeZoneLike_.[[TimeZone]].
1. If ? HasProperty(_temporalTimeZoneLike_, *"timeZone"*) is *false*, return _temporalTimeZoneLike_.
1. Set _temporalTimeZoneLike_ to ? Get(_temporalTimeZoneLike_, *"timeZone"*).
1. If Type(_temporalTimeZoneLike_) is Object and ? HasProperty(_temporalTimeZoneLike_, *"timeZone"*) is *false*, return _temporalTimeZoneLike_.
1. Let _identifier_ be ? ToString(_temporalTimeZoneLike_).
1. Return ? TimeZoneFrom(_identifier_).
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-timezonefrom" aoid="TimeZoneFrom">
<h1>TimeZoneFrom ( _identifier_ )</h1>
<emu-alg>
1. If Type(_item_) is Object, then
1. If ? HasProperty(_item_, *"timeZone"*) is *false*, return _item_.
1. Set _item_ to ? Get(_item_, *"timeZone"*).
1. If Type(_item_) is Object and ? HasProperty(_item_, *"timeZone"*) is *false*, return _item_.
1. Let _string_ be ? ToString(_item_).
1. Let _result_ be ? ParseTemporalTimeZone(_string_).
1. Let _result_ be ? ParseTemporalTimeZone(_identifier_).
1. Return ? CreateTemporalTimeZone(_result_).
</emu-alg>
</emu-clause>
Expand Down
2 changes: 1 addition & 1 deletion spec/zoneddatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,7 @@ <h1>ToTemporalZonedDateTime ( _item_ [ , _options_ ] )</h1>
1. Let _string_ be ? ToString(_item_).
1. Let _result_ be ? ParseTemporalZonedDateTimeString(_string_).
1. If _result_.[[TimeZoneName]] is *undefined*, throw a *RangeError* exception.
1. Let _timeZone_ be ? TimeZoneFrom(_result_.[[TimeZoneName]]).
1. Let _timeZone_ be ? CreateTemporalTimeZone(_result_.[[TimeZoneName]]).
1. Let _offsetString_ be _result_.[[TimeZoneOffsetString]].
1. Let _calendar_ be ? ToOptionalTemporalCalendar(_result_.[[Calendar]]).
1. Let _offsetNanoseconds_ be ? ParseTimeZoneOffsetString(_offsetString_).
Expand Down

0 comments on commit 3119ab8

Please sign in to comment.