Skip to content

Commit

Permalink
Fast-path conversion of Temporal.ZonedDateTime to PlainDate/Time/Date…
Browse files Browse the repository at this point in the history
…Time

If passing a Temporal.ZonedDateTime where a Temporal.PlainDate,
Temporal.PlainTime, or Temporal.PlainDateTime is expected, then first call
the built-in TimeZone.getPlainDateTimeFor() method to obtain a
Temporal.PlainDateTime object and proceed accordingly.
Previously, the ZonedDateTime's getters would have been called,
potentially calling the getPlainDateTimeFor() method multiple times; very
inefficient.

See: #1428
  • Loading branch information
ptomato committed Apr 16, 2021
1 parent 2122f4c commit 1542e31
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
21 changes: 21 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,13 @@ export const ES = ObjectAssign({}, ES2020, {
ToTemporalDate: (item, options = {}) => {
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalDate(item)) return item;
if (ES.IsTemporalZonedDateTime(item)) {
item = ES.BuiltinTimeZoneGetPlainDateTimeFor(
GetSlot(item, TIME_ZONE),
GetSlot(item, INSTANT),
GetSlot(item, CALENDAR)
);
}
if (ES.IsTemporalDateTime(item)) {
const TemporalPlainDate = GetIntrinsic('%Temporal.PlainDate%');
return new TemporalPlainDate(
Expand Down Expand Up @@ -1197,6 +1204,13 @@ export const ES = ObjectAssign({}, ES2020, {
let year, month, day, hour, minute, second, millisecond, microsecond, nanosecond, calendar;
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalDateTime(item)) return item;
if (ES.IsTemporalZonedDateTime(item)) {
return ES.BuiltinTimeZoneGetPlainDateTimeFor(
GetSlot(item, TIME_ZONE),
GetSlot(item, INSTANT),
GetSlot(item, CALENDAR)
);
}
if (ES.IsTemporalDate(item)) {
const TemporalPlainDateTime = GetIntrinsic('%Temporal.PlainDateTime%');
return new TemporalPlainDateTime(
Expand Down Expand Up @@ -1354,6 +1368,13 @@ export const ES = ObjectAssign({}, ES2020, {
let hour, minute, second, millisecond, microsecond, nanosecond, calendar;
if (ES.Type(item) === 'Object') {
if (ES.IsTemporalTime(item)) return item;
if (ES.IsTemporalZonedDateTime(item)) {
item = ES.BuiltinTimeZoneGetPlainDateTimeFor(
GetSlot(item, TIME_ZONE),
GetSlot(item, INSTANT),
GetSlot(item, CALENDAR)
);
}
if (ES.IsTemporalDateTime(item)) {
const TemporalPlainTime = GetIntrinsic('%Temporal.PlainTime%');
return new TemporalPlainTime(
Expand Down
4 changes: 4 additions & 0 deletions spec/plaindate.html
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,10 @@ <h1>ToTemporalDate ( _item_ [ , _options_ ] )</h1>
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalDate]] internal slot, then
1. Return _item_.
1. If _item_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Let _instant_ be ! CreateTemporalInstant(_item_.[[Nanoseconds]]).
1. Let _plainDateTime_ be ? BuiltinTimeZoneGetPlainDateTimeFor(_item_.[[TimeZone]], _instant_, _item_.[[Calendar]]).
1. Return ! CreateTemporalDate(_plainDateTime_.[[ISOYear]], _plainDateTime_.[[ISOMonth]], _plainDateTime_.[[ISODay]], _plainDateTime_.[[Calendar]]).
1. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
1. Return ! CreateTemporalDate(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], _item_.[[Calendar]]).
1. Let _calendar_ be ? GetOptionalTemporalCalendar(_item_).
Expand Down
3 changes: 3 additions & 0 deletions spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ <h1>ToTemporalDateTime ( _item_ [ , _options_ ] )</h1>
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
1. Return _item_.
1. If _item_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Let _instant_ be ! CreateTemporalInstant(_item_.[[Nanoseconds]]).
1. Return ? BuiltinTimeZoneGetPlainDateTimeFor(_item_.[[TimeZone]], _instant_, _item_.[[Calendar]]).
1. If _item_ has an [[InitializedTemporalDate]] internal slot, then
1. Return ? CreateTemporalDateTime(_item_.[[ISOYear]], _item_.[[ISOMonth]], _item_.[[ISODay]], 0, 0, 0, 0, 0, 0, _item_.[[Calendar]]).
1. Let _calendar_ be ? GetOptionalTemporalCalendar(_item_).
Expand Down
4 changes: 4 additions & 0 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,10 @@ <h1>ToTemporalTime ( _item_ [ , _overflow_ ] )</h1>
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalTime]] internal slot, then
1. Return _item_.
1. If _item_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Let _instant_ be ? CreateTemporalInstant(_item_.[[Nanoseconds]]).
1. Set _plainDateTime_ to ? BuiltinTimeZoneGetPlainDateTimeFor(_item_.[[TimeZone]], _instant_, _item_.[[Calendar]]).
1. Return ! CreateTemporalTime(_plainDateTime_.[[ISOHour]], _plainDateTime_.[[ISOMinute]], _plainDateTime_.[[ISOSecond]], _plainDateTime_.[[ISOMillisecond]], _plainDateTime_.[[ISOMicrosecond]], _plainDateTime_.[[ISONanosecond]]).
1. If _item_ has an [[InitializedTemporalDateTime]] internal slot, then
1. Return ! CreateTemporalTime(_item_.[[ISOHour]], _item_.[[ISOMinute]], _item_.[[ISOSecond]], _item_.[[ISOMillisecond]], _item_.[[ISOMicrosecond]], _item_.[[ISONanosecond]]).
1. Let _calendar_ be ? GetOptionalTemporalCalendar(_item_).
Expand Down

0 comments on commit 1542e31

Please sign in to comment.