Skip to content

Commit 36f7d13

Browse files
committed
Polyfill: Implement ISODateTimeWithinLimits assertions
See #3015, which posits that these assertions are hit in existing test262 tests.
1 parent b1acdc8 commit 36f7d13

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2762,6 +2762,15 @@ export function RejectDateTimeRange(isoDateTime) {
27622762
}
27632763
}
27642764

2765+
// Same as above, but throws a different, non-user-facing error
2766+
function AssertISODateTimeWithinLimits(isoDateTime) {
2767+
const ns = GetUTCEpochNanoseconds(isoDateTime);
2768+
assert(
2769+
ns.geq(DATETIME_NS_MIN) && ns.leq(DATETIME_NS_MAX),
2770+
`${ISODateTimeToString(isoDateTime)} is outside the representable range`
2771+
);
2772+
}
2773+
27652774
// In the spec, IsValidEpochNanoseconds returns a boolean and call sites are
27662775
// responsible for throwing. In the polyfill, ValidateEpochNanoseconds takes its
27672776
// place so that we can DRY the throwing code.
@@ -3000,6 +3009,8 @@ function DifferenceInstant(ns1, ns2, increment, smallestUnit, roundingMode) {
30003009
}
30013010

30023011
function DifferenceISODateTime(isoDateTime1, isoDateTime2, calendar, largestUnit) {
3012+
AssertISODateTimeWithinLimits(isoDateTime1);
3013+
AssertISODateTimeWithinLimits(isoDateTime2);
30033014
let timeDuration = DifferenceTime(isoDateTime1.time, isoDateTime2.time);
30043015

30053016
const timeSign = timeDuration.sign();
@@ -4006,13 +4017,12 @@ export function RoundTemporalInstant(epochNs, increment, unit, roundingMode) {
40064017
return RoundNumberToIncrementAsIfPositive(epochNs, incrementNs, roundingMode);
40074018
}
40084019

4009-
export function RoundISODateTime(
4010-
{ isoDate: { year, month, day }, time: { hour, minute, second, millisecond, microsecond, nanosecond } },
4011-
increment,
4012-
unit,
4013-
roundingMode
4014-
) {
4015-
const time = RoundTime({ hour, minute, second, millisecond, microsecond, nanosecond }, increment, unit, roundingMode);
4020+
export function RoundISODateTime(isoDateTime, increment, unit, roundingMode) {
4021+
AssertISODateTimeWithinLimits(isoDateTime);
4022+
const {
4023+
isoDate: { year, month, day }
4024+
} = isoDateTime;
4025+
const time = RoundTime(isoDateTime.time, increment, unit, roundingMode);
40164026
const isoDate = BalanceISODate(year, month, day + time.deltaDays);
40174027
return CombineISODateAndTimeRecord(isoDate, time);
40184028
}

0 commit comments

Comments
 (0)