Bug 1892041 - Part 1: Update test262 features. r=spidermonkey-reviewers,dminor
[gecko.git] / js / src / tests / test262 / built-ins / Temporal / Duration / prototype / subtract / relativeto-zoneddatetime-normalized-time-duration-to-days-range-errors.js
blob46307bcaf93b4cfc5144ece5ed87b22e99b9a346
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally
2 // Copyright (C) 2022 Igalia, S.L. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4 /*---
5 esid: sec-temporal.duration.prototype.subtract
6 description: >
7   Abstract operation NormalizedTimeDurationToDays can throw four different
8   RangeErrors.
9 info: |
10   NormalizedTimeDurationToDays ( norm, zonedRelativeTo, timeZoneRec [ , precalculatedPlainDateTime ] )
11     22. If days < 0 and sign = 1, throw a RangeError exception.
12     23. If days > 0 and sign = -1, throw a RangeError exception.
13     ...
14     25. If NormalizedTimeDurationSign(_norm_) = 1 and sign = -1, throw a RangeError exception.
15     ...
16     28. If dayLength ≥ 2⁵³, throw a RangeError exception.
17 features: [Temporal, BigInt]
18 includes: [temporalHelpers.js]
19 ---*/
21 const dayNs = 86_400_000_000_000;
22 const dayDuration = Temporal.Duration.from({ days: 1 });
23 const epochInstant = new Temporal.Instant(0n);
25 function timeZoneSubstituteValues(
26   getPossibleInstantsFor,
27   getOffsetNanosecondsFor
28 ) {
29   const tz = new Temporal.TimeZone("UTC");
30   TemporalHelpers.substituteMethod(
31     tz,
32     "getPossibleInstantsFor",
33     getPossibleInstantsFor
34   );
35   TemporalHelpers.substituteMethod(
36     tz,
37     "getOffsetNanosecondsFor",
38     getOffsetNanosecondsFor
39   );
40   return tz;
43 // Step 22: days < 0 and sign = 1
44 let zdt = new Temporal.ZonedDateTime(
45   -1n, // Set DifferenceZonedDateTime _ns1_
46   timeZoneSubstituteValues(
47     [
48       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for first call, AddDuration step 15
49       [epochInstant], // Returned in AddDuration step 16, setting _endNs_ -> DifferenceZonedDateTime _ns2_
50       [epochInstant], // Returned in step 16, setting _relativeResult_
51     ],
52     [
53       // Behave normally in 3 calls made prior to NormalizedTimeDurationToDays
54       TemporalHelpers.SUBSTITUTE_SKIP,
55       TemporalHelpers.SUBSTITUTE_SKIP,
56       TemporalHelpers.SUBSTITUTE_SKIP,
57       dayNs - 1, // Returned in step 8, setting _startDateTime_
58       -dayNs + 1, // Returned in step 9, setting _endDateTime_
59     ]
60   )
62 assert.throws(RangeError, () =>
63   // Subtracting day from day sets largestUnit to 'day', avoids having any week/month/year components in difference
64   dayDuration.subtract(dayDuration, {
65     relativeTo: zdt,
66   })
69 // Step 23: days > 0 and sign = -1
70 zdt = new Temporal.ZonedDateTime(
71   1n, // Set DifferenceZonedDateTime _ns1_
72   timeZoneSubstituteValues(
73     [
74       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for first call, AddDuration step 15
75       [epochInstant], // Returned in AddDuration step 16, setting _endNs_ -> DifferenceZonedDateTime _ns2_
76       [epochInstant], // Returned in step 16, setting _relativeResult_
77     ],
78     [
79       // Behave normally in 3 calls made prior to NanosecondsToDays
80       TemporalHelpers.SUBSTITUTE_SKIP,
81       TemporalHelpers.SUBSTITUTE_SKIP,
82       TemporalHelpers.SUBSTITUTE_SKIP,
83       -dayNs + 1, // Returned in step 8, setting _startDateTime_
84       dayNs - 1, // Returned in step 9, setting _endDateTime_
85     ]
86   )
88 assert.throws(RangeError, () =>
89   // Subtracting day from day sets largestUnit to 'day', avoids having any week/month/year components in difference
90   dayDuration.subtract(dayDuration, {
91     relativeTo: zdt,
92   })
95 // Step 25: nanoseconds > 0 and sign = -1
96 zdt = new Temporal.ZonedDateTime(
97   0n, // Set DifferenceZonedDateTime _ns1_
98   timeZoneSubstituteValues(
99     [
100       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for first call, AddDuration step 15
101       [new Temporal.Instant(-1n)], // Returned in AddDuration step 16, setting _endNs_ -> DifferenceZonedDateTime _ns2_
102       [new Temporal.Instant(-2n)], // Returned in step 16, setting _relativeResult_
103       [new Temporal.Instant(-4n)], // Returned in step 21.a, setting _oneDayFarther_
104     ],
105     [
106       // Behave normally in 3 calls made prior to NanosecondsToDays
107       TemporalHelpers.SUBSTITUTE_SKIP,
108       TemporalHelpers.SUBSTITUTE_SKIP,
109       TemporalHelpers.SUBSTITUTE_SKIP,
110       dayNs - 1, // Returned in step 8, setting _startDateTime_
111       -dayNs + 1, // Returned in step 9, setting _endDateTime_
112     ]
113   )
115 assert.throws(RangeError, () =>
116   // Subtracting day from day sets largestUnit to 'day', avoids having any week/month/year components in difference
117   dayDuration.subtract(dayDuration, {
118     relativeTo: zdt,
119   })
122 // Step 28: day length is an unsafe integer
123 zdt = new Temporal.ZonedDateTime(
124   0n,
125   timeZoneSubstituteValues(
126     [
127       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for AddDuration step 15
128       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for AddDuration step 16
129       TemporalHelpers.SUBSTITUTE_SKIP, // Behave normally for step 16, setting _relativeResult_
130       // Returned in step 21.a, making _oneDayFarther_ 2^53 ns later than _relativeResult_
131       [new Temporal.Instant(2n ** 53n - 3n * BigInt(dayNs))],
132     ],
133     []
134   )
136 const twoDaysDuration = new Temporal.Duration(0, 0, 0, 2);
137 assert.throws(RangeError, () =>
138   dayDuration.subtract(twoDaysDuration, {
139     relativeTo: zdt,
140   }),
141   "Should throw RangeError when time zone calculates an outrageous day length"
144 reportCompare(0, 0);