Skip to content

Commit

Permalink
Fast-path conversion of Temporal.ZonedDateTime to Temporal.Instant
Browse files Browse the repository at this point in the history
If passing a Temporal.ZonedDateTime where a Temporal.Instant is expected,
then create a Temporal.Instant directly from the ZonedDateTime's
[[Nanoseconds]] internal slot.
Previously, the ZonedDateTime's toString() method would have been called,
and the result would have been parsed into an Instant.

See: #1428
  • Loading branch information
ptomato committed Apr 13, 2021
1 parent 13921eb commit e836f82
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 4 additions & 0 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,10 @@ export const ES = ObjectAssign({}, ES2020, {
},
ToTemporalInstant: (item) => {
if (ES.IsTemporalInstant(item)) return item;
if (ES.IsTemporalZonedDateTime(item)) {
const TemporalInstant = GetIntrinsic('%Temporal.Instant%');
return new TemporalInstant(GetSlot(item, EPOCHNANOSECONDS));
}
const ns = ES.ParseTemporalInstant(ES.ToString(item));
const TemporalInstant = GetIntrinsic('%Temporal.Instant%');
return new TemporalInstant(ns);
Expand Down
7 changes: 5 additions & 2 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -519,8 +519,11 @@ <h1>ToTemporalInstant ( _item_ )</h1>
The abstract operation ToTemporalInstant returns its argument _item_ if it is already a Temporal.Instant instance, converts _item_ to a new Temporal.Instant instance if possible, and throws otherwise.
</p>
<emu-alg>
1. If Type(_item_) is Object and _item_ has an [[InitializedTemporalInstant]] internal slot, then
1. Return _item_.
1. If Type(_item_) is Object, then
1. If _item_ has an [[InitializedTemporalInstant]] internal slot, then
1. Return _item_.
1. If _item_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Return ! CreateTemporalInstant(_item_.[[Nanoseconds]]).
1. Let _string_ be ? ToString(_item_).
1. Let _epochNanoseconds_ be ? ParseTemporalInstant(_string_).
1. Return ? CreateTemporalInstant(_epochNanoseconds_).
Expand Down

0 comments on commit e836f82

Please sign in to comment.