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.
6 esid: sec-temporal-zoneddatetime-objects
7 description: Temporal.ZonedDateTime.prototype.round()
8 includes: [temporalHelpers.js]
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({
24 // throws on disallowed or invalid smallestUnit (string param)
34 ].forEach(smallestUnit => {
35 assert.throws(RangeError, () => zdt.round(smallestUnit));
38 // rounds to an increment of hours
39 assert.sameValue(`${ zdt.round({
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",
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",
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",
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",
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",
72 }) }`, "1976-11-18T15:23:30.12345679+01:00[+01:00]");
74 // 1 day is a valid increment
75 assert.sameValue(`${ zdt.round({
78 }) }`, "1976-11-19T00:00:00+01:00[+01:00]");
80 // valid hour increments divide into 24
81 var smallestUnit = "hour";
90 ].forEach(roundingIncrement => {
94 }) instanceof Temporal.ZonedDateTime);
99 ].forEach(smallestUnit => {
100 // valid minutes/seconds increments divide into 60`, () => {
113 ].forEach(roundingIncrement => {
117 }) instanceof Temporal.ZonedDateTime);
124 ].forEach(smallestUnit => {
125 // valid increments divide into 1000`
142 ].forEach(roundingIncrement => {
146 }) instanceof Temporal.ZonedDateTime);
150 // throws on increments that do not divide evenly into the next highest
151 assert.throws(RangeError, () => zdt.round({
153 roundingIncrement: 29
155 assert.throws(RangeError, () => zdt.round({
156 smallestUnit: "hour",
157 roundingIncrement: 29
159 assert.throws(RangeError, () => zdt.round({
160 smallestUnit: "minute",
161 roundingIncrement: 29
163 assert.throws(RangeError, () => zdt.round({
164 smallestUnit: "second",
165 roundingIncrement: 29
167 assert.throws(RangeError, () => zdt.round({
168 smallestUnit: "millisecond",
169 roundingIncrement: 29
171 assert.throws(RangeError, () => zdt.round({
172 smallestUnit: "microsecond",
173 roundingIncrement: 29
175 assert.throws(RangeError, () => zdt.round({
176 smallestUnit: "nanosecond",
177 roundingIncrement: 29
180 // throws on increments that are equal to the next highest
181 assert.throws(RangeError, () => zdt.round({
182 smallestUnit: "hour",
183 roundingIncrement: 24
185 assert.throws(RangeError, () => zdt.round({
186 smallestUnit: "minute",
187 roundingIncrement: 60
189 assert.throws(RangeError, () => zdt.round({
190 smallestUnit: "second",
191 roundingIncrement: 60
193 assert.throws(RangeError, () => zdt.round({
194 smallestUnit: "millisecond",
195 roundingIncrement: 1000
197 assert.throws(RangeError, () => zdt.round({
198 smallestUnit: "microsecond",
199 roundingIncrement: 1000
201 assert.throws(RangeError, () => zdt.round({
202 smallestUnit: "nanosecond",
203 roundingIncrement: 1000
205 var bal = Temporal.ZonedDateTime.from("1976-11-18T23:59:59.999999999+01:00[+01:00]");
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);