From d7aec8e178369bec755d308adb0613d760a7c864 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Mon, 11 Mar 2024 18:19:33 -0700 Subject: [PATCH] Temporal: Add test for Duration going out of range after rounding After rounding relative to a ZonedDateTime, we have to potentially adjust for DST. With a time zone providing nonsensical values, the duration may go out of range. See: tc39/proposal-temporal#2801 --- ...ut-of-range-when-adjusting-rounded-days.js | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/built-ins/Temporal/Duration/prototype/round/out-of-range-when-adjusting-rounded-days.js diff --git a/test/built-ins/Temporal/Duration/prototype/round/out-of-range-when-adjusting-rounded-days.js b/test/built-ins/Temporal/Duration/prototype/round/out-of-range-when-adjusting-rounded-days.js new file mode 100644 index 0000000000..e88abfd374 --- /dev/null +++ b/test/built-ins/Temporal/Duration/prototype/round/out-of-range-when-adjusting-rounded-days.js @@ -0,0 +1,29 @@ +// Copyright (C) 2024 Igalia, S.L. All rights reserved. +// This code is governed by the BSD license found in the LICENSE file. + +/*--- +esid: sec-temporal.duration.prototype.round +description: > + When adjusting the rounded days after rounding relative to a ZonedDateTime, + the duration may go out of range +features: [Temporal] +includes: [temporalHelpers.js] +---*/ + +// Based on a test case by André Bargull + +const d = new Temporal.Duration(0, 0, 0, /* days = */ 1, 0, 0, /* s = */ Number.MAX_SAFE_INTEGER - 86400, 0, 0, /* ns = */ 999_999_999); + +const timeZone = new Temporal.TimeZone("UTC"); +TemporalHelpers.substituteMethod(timeZone, 'getPossibleInstantsFor', [ + TemporalHelpers.SUBSTITUTE_SKIP, + [new Temporal.Instant(1n)], +]); + +const relativeTo = new Temporal.ZonedDateTime(0n, timeZone); + +assert.throws(RangeError, () => d.round({ + largestUnit: 'nanoseconds', + roundingIncrement: 2, + relativeTo +}));