1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally
2 // Copyright (C) 2024 Igalia, S.L. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
5 esid: sec-temporal.duration.prototype.add
7 Throws a RangeError when custom calendar method returns inconsistent result
9 DifferenceZonedDateTime ( ... )
12 g. If _sign_ = 0, or _timeSign_ = 0, or _sign_ = _timeSign_, then
14 viii. Return ? CreateNormalizedDurationRecord(_dateDifference_.[[Years]],
15 _dateDifference_.[[Months]], _dateDifference_.[[Weeks]],
16 _dateDifference_.[[Days]], _norm_).
17 h. Set _dayCorrection_ to _dayCorrection_ + 1.
18 9. NOTE: This step is only reached when custom calendar or time zone methods
19 return inconsistent values.
20 10. Throw a *RangeError* exception.
24 // Based partly on a test case by André Bargull
26 const duration1 = new Temporal.Duration(0, 0, /* weeks = */ 7, 0, /* hours = */ 12);
27 const duration2 = new Temporal.Duration(0, 0, 0, /* days = */ 1);
30 const tz = new (class extends Temporal.TimeZone {
31 getPossibleInstantsFor(dateTime) {
32 return super.getPossibleInstantsFor(dateTime.add({ days: 3 }));
36 const relativeTo = new Temporal.ZonedDateTime(0n, tz);
38 assert.throws(RangeError, () => duration1.add(duration2, { relativeTo }),
39 "Calendar calculation where more than 2 days correction is needed should cause RangeError");
43 const cal = new (class extends Temporal.Calendar {
44 dateUntil(one, two, options) {
45 return super.dateUntil(one, two, options).negated();
49 const relativeTo = new Temporal.ZonedDateTime(0n, "UTC", cal);
51 assert.throws(RangeError, () => duration1.add(duration2, { relativeTo }),
52 "Calendar calculation causing mixed-sign values should cause RangeError");