Skip to content

Commit 95e32f6

Browse files
committed
Temporal: Add tests for some Temporal.Duration.p.round edge cases
Found these using snapshot testing. roundingincrement-days-large.js tests a failure mode that can happen if using floating-points to convert large rounding increments to nanoseconds. case-where-relativeto-affects-rounding-mode-half-even.js tests an edge case that is somewhat surprising, but correct as per the specification.
1 parent dc0a351 commit 95e32f6

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2026 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.round
6+
description: >
7+
Rare edge case where presence or absence of relativeTo affects the rounding
8+
behaviour of rounding mode halfEven
9+
features: [Temporal]
10+
includes: [temporalHelpers.js]
11+
---*/
12+
13+
const plainRelativeTo = new Temporal.PlainDate(1970, 1, 1);
14+
const zonedRelativeTo = new Temporal.ZonedDateTime(0n, "UTC");
15+
16+
const duration = new Temporal.Duration(0, 0, 0, 3, 12); // 3 days 12 hours
17+
const commonOptions = { smallestUnit: "hours", roundingIncrement: 8, roundingMode: "halfEven" };
18+
19+
// 3 days 12 hours is 10.5 increments, so halfEven rounds down to 10 increments
20+
TemporalHelpers.assertDuration(
21+
duration.round(commonOptions),
22+
0, 0, 0, 3, 8, 0, 0, 0, 0, 0, // 3 days 8 hours
23+
"halfEven rounding is downward with no relativeTo"
24+
);
25+
26+
// Here also we calculate 10.5 increments
27+
TemporalHelpers.assertDuration(
28+
duration.round({ ...commonOptions, relativeTo: plainRelativeTo }),
29+
0, 0, 0, 3, 8, 0, 0, 0, 0, 0, // 3 days 8 hours
30+
"halfEven rounding is downward with PlainDate relativeTo"
31+
);
32+
33+
// Since days can be different lengths when relative to ZonedDateTime, the days
34+
// are accounted separately. 0 days 12 hours is 1.5 increments, so halfEven
35+
// rounds up to 2 increments
36+
TemporalHelpers.assertDuration(
37+
duration.round({ ...commonOptions, relativeTo: zonedRelativeTo }),
38+
0, 0, 0, 3, 16, 0, 0, 0, 0, 0, // 3 days 16 hours
39+
"halfEven rounding is upward with ZonedDateTime relativeTo"
40+
);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2026 Igalia, S.L. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.duration.prototype.round
6+
description: Calculation with a large, but not too large, roundingIncrement
7+
features: [Temporal]
8+
includes: [temporalHelpers.js]
9+
---*/
10+
11+
const plainRelativeTo = new Temporal.PlainDate(1970, 1, 1);
12+
const zonedRelativeTo = new Temporal.ZonedDateTime(0n, "UTC");
13+
14+
const relativeToTests = [
15+
[undefined, 'no'],
16+
[plainRelativeTo, 'plain'],
17+
[zonedRelativeTo, 'zoned'],
18+
];
19+
20+
const duration1 = new Temporal.Duration(0, 0, 0, 0, 0, 0, 9007199254, 740, 991, 0);
21+
for (const [relativeTo, descr] of relativeToTests) {
22+
const result = duration1.round({ smallestUnit: 'days', roundingIncrement: 1e7, relativeTo });
23+
TemporalHelpers.assertDuration(result, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, `round to 1e7 days with ${descr} relativeTo`);
24+
}
25+
26+
const duration2 = new Temporal.Duration(0, 0, 0, 1);
27+
for (const [relativeTo, descr] of relativeToTests) {
28+
const result = duration2.round({ smallestUnit: 'days', roundingIncrement: 1e8 - 1, roundingMode: 'ceil', relativeTo });
29+
TemporalHelpers.assertDuration(result, 0, 0, 0, 99999999, 0, 0, 0, 0, 0, 0, `round to 1e8-1 days with ${descr} relativeTo`);
30+
}

0 commit comments

Comments
 (0)