Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,21 @@ TemporalHelpers.assertInstantsEqual(inst.round({
smallestUnit: "nanosecond",
roundingIncrement: 10
}), Temporal.Instant.from("1976-11-18T14:23:30.12345679Z"));

const unitsAndIncrements =
{"hour": [1, 2, 4, 6, 8, 12, 24],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: please format these consistently between files

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 99c46d3

"minute": [1, 5, 10, 20, 30, 40, 80, 120, 720, 1440],
"second": [1, 5, 10, 20, 25, 30, 50, 100, 400, 86400],
"millisecond": [1, 5, 10, 20, 25, 30, 50, 100, 86_400_000],
"microsecond": [1, 5, 10, 20, 25, 30, 50, 100],
"nanosecond": [1, 5, 10, 20, 25, 30, 50, 100]
};

// Just check that each combination of unit and increment doesn't throw
Object.entries(unitsAndIncrements).forEach(([unit, increments]) => {
increments.forEach((increment) => {
const result = inst.round({ smallestUnit: unit, roundingMode: "ceil", roundingIncrement: increment });
assert.sameValue(result instanceof Temporal.Instant, true, `${unit} ${increment}`);
})
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2025 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.plaintime.prototype.round
description: Rounding increment should properly divide the relevant time unit
features: [Temporal]
---*/

const t = new Temporal.PlainTime(14, 23, 30, 123, 456, 789);

[1, 2, 3, 4, 6, 8, 12].forEach((roundingIncrement) => {
assert.sameValue(
t.round({ smallestUnit: "hour", roundingIncrement }) instanceof Temporal.PlainDateTime,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure these shouldn't be PDTs. And probably this test would benefit from using the unitsAndIncrements table as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, fixed in 99c46d3; also changed it to use a table.

true,
`valid hour increments divide into 24 (rounding increment = ${roundingIncrement})`);
});

["minute", "second"].forEach((smallestUnit) => {
[1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30].forEach((roundingIncrement) => {
assert.sameValue(
t.round({ smallestUnit, roundingIncrement }) instanceof Temporal.PlainDateTime,
true,
`valid ${smallestUnit} increments divide into 60 (rounding increment = ${roundingIncrement})`
);
});
});

["millisecond", "microsecond", "nanosecond"].forEach((smallestUnit) => {
[1, 2, 4, 5, 8, 10, 20, 25, 40, 50, 100, 125, 200, 250, 500].forEach((roundingIncrement) => {
assert.sameValue(
t.round({ smallestUnit, roundingIncrement }) instanceof Temporal.PlainDateTime,
true,
`valid ${smallestUnit} increments divide into 1000 (rounding increment = ${roundingIncrement})`);
});
});

const nextIncrements = {
"hour": 24,
"minute": 60,
"second": 60,
"millisecond": 1000,
"microsecond": 1000,
"nanosecond": 1000
};

Object.entries(nextIncrements).forEach(([unit, next]) => {
assert.throws(
RangeError,
() => t.round({ smallestUnit: unit, roundingIncrement: next }),
`throws on increments that are equal to the next highest (unit = ${unit}, increment = ${next})`
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,19 @@ TemporalHelpers.assertZonedDateTimesEqual(zdt.round({
roundingIncrement: 1
}), expected1Day);

const unitsAndIncrements = {
"hour": [1, 2, 4, 6, 8, 12],
"minute": [1, 3, 5, 6, 10, 30],
"second": [1, 3, 5, 6, 10, 30],
"millisecond": [1, 5, 10, 20, 25, 50, 100, 500],
"microsecond": [1, 5, 10, 20, 25, 50, 100, 500],
"nanosecond": [1, 5, 10, 20, 25, 50, 100, 500],
};

// Just check that each combination of unit and increment doesn't throw
Object.entries(unitsAndIncrements).forEach(([unit, increments]) => {
increments.forEach((increment) => {
const result = zdt.round({ smallestUnit: unit, roundingMode: "ceil", roundingIncrement: increment });
assert.sameValue(result instanceof Temporal.ZonedDateTime, true, `${unit} ${increment}`);
})
});