Skip to content

Commit

Permalink
Editorial: Allow passing pre-looked-up TimeZone methods
Browse files Browse the repository at this point in the history
Just as in CalendarDateAdd and CalendarDateUntil, add optional parameters
to GetPossibleInstantsFor and GetOffsetNanosecondsFor which can supply the
method to be called, in case the caller has already looked it up.

This doesn't make any observable change, but it will be used for
optimizations in #2519.
  • Loading branch information
ptomato committed Apr 26, 2023
1 parent ca103d6 commit c09dae3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
8 changes: 4 additions & 4 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,13 +2135,13 @@ export function TemporalDateTimeToTime(dateTime) {
);
}

export function GetOffsetNanosecondsFor(timeZone, instant) {
export function GetOffsetNanosecondsFor(timeZone, instant, getOffsetNanosecondsFor) {
if (typeof timeZone === 'string') {
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
timeZone = new TemporalTimeZone(timeZone);
return Call(GetIntrinsic('%Temporal.TimeZone.prototype.getOffsetNanosecondsFor%'), timeZone, [instant]);
}
const getOffsetNanosecondsFor = GetMethod(timeZone, 'getOffsetNanosecondsFor');
getOffsetNanosecondsFor ??= GetMethod(timeZone, 'getOffsetNanosecondsFor');
const offsetNs = Call(getOffsetNanosecondsFor, timeZone, [instant]);
if (typeof offsetNs !== 'number') {
throw new TypeError('bad return from getOffsetNanosecondsFor');
Expand Down Expand Up @@ -2306,13 +2306,13 @@ export function DisambiguatePossibleInstants(possibleInstants, timeZone, dateTim
throw new Error(`assertion failed: invalid disambiguation value ${disambiguation}`);
}

export function GetPossibleInstantsFor(timeZone, dateTime) {
export function GetPossibleInstantsFor(timeZone, dateTime, getPossibleInstantsFor = undefined) {
if (typeof timeZone === 'string') {
const TemporalTimeZone = GetIntrinsic('%Temporal.TimeZone%');
timeZone = new TemporalTimeZone(timeZone);
return Call(GetIntrinsic('%Temporal.TimeZone.prototype.getPossibleInstantsFor%'), timeZone, [dateTime]);
}
const getPossibleInstantsFor = GetMethod(timeZone, 'getPossibleInstantsFor');
getPossibleInstantsFor ??= GetMethod(timeZone, 'getPossibleInstantsFor');
const possibleInstants = Call(getPossibleInstantsFor, timeZone, [dateTime]);
const result = [];
for (const instant of possibleInstants) {
Expand Down
40 changes: 33 additions & 7 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -635,13 +635,26 @@ <h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-getoffsetnanosecondsfor" aoid="GetOffsetNanosecondsFor">
<h1>GetOffsetNanosecondsFor ( _timeZone_, _instant_ )</h1>
<emu-clause id="sec-temporal-getoffsetnanosecondsfor" type="abstract operation">
<h1>
GetOffsetNanosecondsFor (
_timeZone_: a String or Object,
_instant_: a Temporal.Instant,
optional _getOffsetNanosecondsFor_: a function object,
): either a normal completion containing an integer, or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>
It determines the UTC offset of an _instant_, in nanoseconds, by calling the `getOffsetNanosecondsFor` method of the given _timeZone_.
If _getOffsetNanosecondsFor_ is present, the `getOffsetNanosecondsFor` method will not be observably looked up.
</dd>
</dl>
<emu-alg>
1. If _timeZone_ is a String, then
1. Set _timeZone_ to ! CreateTemporalTimeZone(_timeZone_).
1. Return ? Call(%Temporal.TimeZone.prototype.getOffsetNanosecondsFor%, _timeZone_, « _instant_ »).
1. Let _getOffsetNanosecondsFor_ be ? GetMethod(_timeZone_, *"getOffsetNanosecondsFor"*).
1. If _getOffsetNanosecondsFor_ is not present, set _getOffsetNanosecondsFor_ to ? GetMethod(_timeZone_, *"getOffsetNanosecondsFor"*).
1. Let _offsetNanoseconds_ be ? Call(_getOffsetNanosecondsFor_, _timeZone_, « _instant_ »).
1. If Type(_offsetNanoseconds_) is not Number, throw a *TypeError* exception.
1. If IsIntegralNumber(_offsetNanoseconds_) is *false*, throw a *RangeError* exception.
Expand Down Expand Up @@ -765,15 +778,28 @@ <h1>DisambiguatePossibleInstants ( _possibleInstants_, _timeZone_, _dateTime_, _
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-getpossibleinstantsfor" aoid="GetPossibleInstantsFor">
<h1>GetPossibleInstantsFor ( _timeZone_, _dateTime_ )</h1>
<emu-clause id="sec-temporal-getpossibleinstantsfor" type="abstract operation">
<h1>
GetPossibleInstantsFor (
_timeZone_: a String or Object,
_dateTime_: a Temporal.PlainDateTime,
optional _getPossibleInstantsFor_: a function object,
): either a normal completion containing a List of Temporal.Instant objects, or an abrupt completion
</h1>
<dl class="header">
<dt>description</dt>
<dd>
It determines the possible Temporal.Instant exact times that may correspond to _dateTime_, by calling the `getPossibleInstantsFor` method of the given _timeZone_.
If _getPossibleInstantsFor_ is present, the `getPossibleInstantsFor` method will not be observably looked up.
</dd>
</dl>
<emu-alg>
1. Assert: _dateTime_ has an [[InitializedTemporalDateTime]] internal slot.
1. If _timeZone_ is a String, then
1. Set _timeZone_ to ! CreateTemporalTimeZone(_timeZone_).
1. Let _array_ be ? Call(%Temporal.TimeZone.prototype.getPossibleInstantsFor%, _timeZone_, « _dateTime_ »).
1. Return ! CreateListFromArrayLike(_array_, « Object »).
1. Let _possibleInstants_ be ? Invoke(_timeZone_, *"getPossibleInstantsFor"*, « _dateTime_ »).
1. If _getPossibleInstantsFor_ is not present, set _getPossibleInstantsFor_ to ? GetMethod(_timeZone_, *"getPossibleInstantsFor"*).
1. Let _possibleInstants_ be ? Call(_timeZone_, *"getPossibleInstantsFor"*, « _dateTime_ »).
1. Let _iteratorRecord_ be ? GetIterator(_possibleInstants_, ~sync~).
1. Let _list_ be a new empty List.
1. Let _next_ be *true*.
Expand Down

0 comments on commit c09dae3

Please sign in to comment.