Skip to content

Commit

Permalink
Ensure SystemUTCEpochNanoseconds never throws
Browse files Browse the repository at this point in the history
Times outside of a range of about half a million years cannot be
represented by Temporal.Instant. Clamp the system time to that range so
that the operation is guaranteed not to throw, which is editorially the
cleanest solution. If your clock is 300,000 years off then Temporal is not
the biggest problem you will have, so this is not expected to come up in
practice.

See: #1424
  • Loading branch information
ptomato committed Apr 17, 2021
1 parent 21395b1 commit 28ed5fa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4315,7 +4315,7 @@ export const ES = ObjectAssign({}, ES2020, {
const ms = Date.now();
const result = bigInt(ms).multiply(1e6).plus(ns);
ns = ms % 1e6;
return result;
return bigInt.min(NS_MAX, bigInt.max(NS_MIN, result));
};
})(),
SystemTimeZone: () => {
Expand Down
12 changes: 9 additions & 3 deletions spec/temporal.html
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,26 @@ <h1>SystemTimeZone ( )</h1>
<h1>SystemUTCEpochNanoseconds ( )</h1>
<emu-alg>
1. Let _ns_ be the approximate current UTC date and time, in nanoseconds since the epoch.
1. Assert: ! ValidateInstant(_ns_) is *true*.
1. Set _ns_ to the result of clamping _ns_ between −8.64 × 10<sup>21</sup> and 8.64 × 10<sup>21</sup>.
1. Return _ns_.
</emu-alg>
<emu-note>
<p>The precision of the result depends on the host. In particular, web browsers artificially
limit it to prevent abuse of security flaws (e.g., Spectre) and to avoid certain methods of
fingerprinting.</p>
</emu-note>
<emu-note>
<p>
Step 2 is necessary if the system clock is set to a time outside the range that Temporal.Instant can represent.
This is not expected to affect implementations in practice.
</p>
</emu-note>
</emu-clause>

<emu-clause id="sec-temporal-systeminstant" aoid="SystemInstant">
<h1>SystemInstant ( )</h1>
<emu-alg>
1. Let _ns_ be ? SystemUTCEpochNanoseconds().
1. Let _ns_ be ! SystemUTCEpochNanoseconds().
1. Return ? CreateTemporalInstant(_ns_).
</emu-alg>
</emu-clause>
Expand All @@ -231,7 +237,7 @@ <h1>SystemZonedDateTime ( [ _temporalTimeZoneLike_ [ , _calendarLike_ ] ] )</h1>
1. Else,
1. Let _timeZone_ be ? ToTemporalTimeZone(_temporalTimeZoneLike_).
1. Let _calendar_ be ? ToTemporalCalendar(_calendarLike_).
1. Let _ns_ be ? SystemUTCEpochNanoseconds().
1. Let _ns_ be ! SystemUTCEpochNanoseconds().
1. Return ? CreateTemporalZonedDateTime(_ns_, _timeZone_, _calendar_).
</emu-alg>
</emu-clause>
Expand Down

0 comments on commit 28ed5fa

Please sign in to comment.