Bug 1892041 - Part 2: Update test262. r=spidermonkey-reviewers,dminor
[gecko.git] / js / src / tests / test262 / staging / Temporal / ZonedDateTime / old / round.js
blob6c613fa47b3bc39d652f827c78aaf9b5a6eb4942
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally
2 // Copyright (C) 2018 Bloomberg LP. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
5 /*---
6 esid: sec-temporal-zoneddatetime-objects
7 description: Temporal.ZonedDateTime.prototype.round()
8 includes: [temporalHelpers.js]
9 features: [Temporal]
10 ---*/
12 var zdt = Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456789+01:00[+01:00]");
14 // throws without parameter
15 assert.throws(TypeError, () => zdt.round());
17 // throws without required smallestUnit parameter
18 assert.throws(RangeError, () => zdt.round({}));
19 assert.throws(RangeError, () => zdt.round({
20   roundingIncrement: 1,
21   roundingMode: "ceil"
22 }));
24 // throws on disallowed or invalid smallestUnit (string param)
26   "era",
27   "year",
28   "month",
29   "week",
30   "years",
31   "months",
32   "weeks",
33   "nonsense"
34 ].forEach(smallestUnit => {
35   assert.throws(RangeError, () => zdt.round(smallestUnit));
36 });
38 // rounds to an increment of hours
39 assert.sameValue(`${ zdt.round({
40   smallestUnit: "hour",
41   roundingIncrement: 4
42 }) }`, "1976-11-18T16:00:00+01:00[+01:00]");
44 // rounds to an increment of minutes
45 assert.sameValue(`${ zdt.round({
46   smallestUnit: "minute",
47   roundingIncrement: 15
48 }) }`, "1976-11-18T15:30:00+01:00[+01:00]");
50 // rounds to an increment of seconds
51 assert.sameValue(`${ zdt.round({
52   smallestUnit: "second",
53   roundingIncrement: 30
54 }) }`, "1976-11-18T15:23:30+01:00[+01:00]");
56 // rounds to an increment of milliseconds
57 assert.sameValue(`${ zdt.round({
58   smallestUnit: "millisecond",
59   roundingIncrement: 10
60 }) }`, "1976-11-18T15:23:30.12+01:00[+01:00]");
62 // rounds to an increment of microseconds
63 assert.sameValue(`${ zdt.round({
64   smallestUnit: "microsecond",
65   roundingIncrement: 10
66 }) }`, "1976-11-18T15:23:30.12346+01:00[+01:00]");
68 // rounds to an increment of nanoseconds
69 assert.sameValue(`${ zdt.round({
70   smallestUnit: "nanosecond",
71   roundingIncrement: 10
72 }) }`, "1976-11-18T15:23:30.12345679+01:00[+01:00]");
74 // 1 day is a valid increment
75 assert.sameValue(`${ zdt.round({
76   smallestUnit: "day",
77   roundingIncrement: 1
78 }) }`, "1976-11-19T00:00:00+01:00[+01:00]");
80 // valid hour increments divide into 24
81 var smallestUnit = "hour";
83   1,
84   2,
85   3,
86   4,
87   6,
88   8,
89   12
90 ].forEach(roundingIncrement => {
91   assert(zdt.round({
92     smallestUnit,
93     roundingIncrement
94   }) instanceof Temporal.ZonedDateTime);
95 });
97   "minute",
98   "second"
99 ].forEach(smallestUnit => {
100   // valid minutes/seconds increments divide into 60`, () => {
101     [
102       1,
103       2,
104       3,
105       4,
106       5,
107       6,
108       10,
109       12,
110       15,
111       20,
112       30
113     ].forEach(roundingIncrement => {
114       assert(zdt.round({
115         smallestUnit,
116         roundingIncrement
117       }) instanceof Temporal.ZonedDateTime);
118     });
119   });
121   "millisecond",
122   "microsecond",
123   "nanosecond"
124 ].forEach(smallestUnit => {
125   // valid increments divide into 1000`
126     [
127       1,
128       2,
129       4,
130       5,
131       8,
132       10,
133       20,
134       25,
135       40,
136       50,
137       100,
138       125,
139       200,
140       250,
141       500
142     ].forEach(roundingIncrement => {
143       assert(zdt.round({
144         smallestUnit,
145         roundingIncrement
146       }) instanceof Temporal.ZonedDateTime);
147     });
148   });
150 // throws on increments that do not divide evenly into the next highest
151 assert.throws(RangeError, () => zdt.round({
152   smallestUnit: "day",
153   roundingIncrement: 29
154 }));
155 assert.throws(RangeError, () => zdt.round({
156   smallestUnit: "hour",
157   roundingIncrement: 29
158 }));
159 assert.throws(RangeError, () => zdt.round({
160   smallestUnit: "minute",
161   roundingIncrement: 29
162 }));
163 assert.throws(RangeError, () => zdt.round({
164   smallestUnit: "second",
165   roundingIncrement: 29
166 }));
167 assert.throws(RangeError, () => zdt.round({
168   smallestUnit: "millisecond",
169   roundingIncrement: 29
170 }));
171 assert.throws(RangeError, () => zdt.round({
172   smallestUnit: "microsecond",
173   roundingIncrement: 29
174 }));
175 assert.throws(RangeError, () => zdt.round({
176   smallestUnit: "nanosecond",
177   roundingIncrement: 29
178 }));
180 // throws on increments that are equal to the next highest
181 assert.throws(RangeError, () => zdt.round({
182   smallestUnit: "hour",
183   roundingIncrement: 24
184 }));
185 assert.throws(RangeError, () => zdt.round({
186   smallestUnit: "minute",
187   roundingIncrement: 60
188 }));
189 assert.throws(RangeError, () => zdt.round({
190   smallestUnit: "second",
191   roundingIncrement: 60
192 }));
193 assert.throws(RangeError, () => zdt.round({
194   smallestUnit: "millisecond",
195   roundingIncrement: 1000
196 }));
197 assert.throws(RangeError, () => zdt.round({
198   smallestUnit: "microsecond",
199   roundingIncrement: 1000
200 }));
201 assert.throws(RangeError, () => zdt.round({
202   smallestUnit: "nanosecond",
203   roundingIncrement: 1000
204 }));
205 var bal = Temporal.ZonedDateTime.from("1976-11-18T23:59:59.999999999+01:00[+01:00]");
207   "day",
208   "hour",
209   "minute",
210   "second",
211   "millisecond",
212   "microsecond"
213 ].forEach(smallestUnit => {
214   assert.sameValue(`${ bal.round({ smallestUnit }) }`, "1976-11-19T00:00:00+01:00[+01:00]");
217 var timeZone = TemporalHelpers.springForwardFallBackTimeZone();
219 // rounds correctly to a 25-hour day
220 // (the 12.5 hour is the halfway point, which is 11:30 local time, since 2:00-2:59 repeats)
221 var roundTo = { smallestUnit: "day" };
222 var roundMeDown = Temporal.PlainDateTime.from("2000-10-29T11:29:59").toZonedDateTime(timeZone);
223 assert.sameValue(`${ roundMeDown.round(roundTo) }`, "2000-10-29T00:00:00-07:00[Custom/Spring_Fall]");
224 var roundMeUp = Temporal.PlainDateTime.from("2000-10-29T11:30:01").toZonedDateTime(timeZone);
225 assert.sameValue(`${ roundMeUp.round(roundTo) }`, "2000-10-30T00:00:00-08:00[Custom/Spring_Fall]");
227 // rounds correctly to a 23-hour day
228 // (the 11.5 hour is the halfway point, which is 12:30 local time, since 2:00-2:59 skips)
229 var roundTo = { smallestUnit: "day" };
230 var roundMeDown = Temporal.PlainDateTime.from("2000-04-02T12:29:59").toZonedDateTime(timeZone);
231 assert.sameValue(`${ roundMeDown.round(roundTo) }`, "2000-04-02T00:00:00-08:00[Custom/Spring_Fall]");
232 var roundMeUp = Temporal.PlainDateTime.from("2000-04-02T12:30:01").toZonedDateTime(timeZone);
233 assert.sameValue(`${ roundMeUp.round(roundTo) }`, "2000-04-03T00:00:00-07:00[Custom/Spring_Fall]");
235 // rounding up to a nonexistent wall-clock time
236 var almostSkipped = Temporal.PlainDateTime.from("2000-04-02T01:59:59.999999999").toZonedDateTime(timeZone);
237 var rounded = almostSkipped.round({
238   smallestUnit: "microsecond",
239   roundingMode: "halfExpand"
241 assert.sameValue(`${ rounded }`, "2000-04-02T03:00:00-07:00[Custom/Spring_Fall]");
242 assert.sameValue(rounded.epochNanoseconds - almostSkipped.epochNanoseconds, 1n);
244 reportCompare(0, 0);