Skip to content

Commit

Permalink
Polyfill: Implement ISODateTimeWithinLimits assertions
Browse files Browse the repository at this point in the history
See #3015, which posits that these assertions are hit in existing test262
tests.
  • Loading branch information
ptomato committed Nov 1, 2024
1 parent b1acdc8 commit 36f7d13
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2762,6 +2762,15 @@ export function RejectDateTimeRange(isoDateTime) {
}
}

// Same as above, but throws a different, non-user-facing error
function AssertISODateTimeWithinLimits(isoDateTime) {
const ns = GetUTCEpochNanoseconds(isoDateTime);
assert(
ns.geq(DATETIME_NS_MIN) && ns.leq(DATETIME_NS_MAX),
`${ISODateTimeToString(isoDateTime)} is outside the representable range`
);
}

// In the spec, IsValidEpochNanoseconds returns a boolean and call sites are
// responsible for throwing. In the polyfill, ValidateEpochNanoseconds takes its
// place so that we can DRY the throwing code.
Expand Down Expand Up @@ -3000,6 +3009,8 @@ function DifferenceInstant(ns1, ns2, increment, smallestUnit, roundingMode) {
}

function DifferenceISODateTime(isoDateTime1, isoDateTime2, calendar, largestUnit) {
AssertISODateTimeWithinLimits(isoDateTime1);
AssertISODateTimeWithinLimits(isoDateTime2);
let timeDuration = DifferenceTime(isoDateTime1.time, isoDateTime2.time);

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

export function RoundISODateTime(
{ isoDate: { year, month, day }, time: { hour, minute, second, millisecond, microsecond, nanosecond } },
increment,
unit,
roundingMode
) {
const time = RoundTime({ hour, minute, second, millisecond, microsecond, nanosecond }, increment, unit, roundingMode);
export function RoundISODateTime(isoDateTime, increment, unit, roundingMode) {
AssertISODateTimeWithinLimits(isoDateTime);
const {
isoDate: { year, month, day }
} = isoDateTime;
const time = RoundTime(isoDateTime.time, increment, unit, roundingMode);
const isoDate = BalanceISODate(year, month, day + time.deltaDays);
return CombineISODateAndTimeRecord(isoDate, time);
}
Expand Down

0 comments on commit 36f7d13

Please sign in to comment.