Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporal: Tests for Duration rounding fix & consolidation #4041

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -17,41 +17,23 @@ const relativeTo = new Temporal.ZonedDateTime(0n, timeZone, calendar);
// Rounding with smallestUnit a calendar unit.
// The calls come from these paths:
// Duration.round() ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// MoveRelativeDate -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
// AddZonedDateTime -> calendar.dateAdd()
// DifferenceZonedDateTimeWithRounding -> RoundRelativeDuration -> NudgeToCalendarUnit ->
// AddDateTime -> calendar.dateAdd() (2x)

const instance1 = new Temporal.Duration(1, 1, 1, 1, 1);
instance1.round({ smallestUnit: "weeks", relativeTo });
const instance = new Temporal.Duration(1, 1, 1, 1, 1);
instance.round({ smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 3, "rounding with calendar smallestUnit");

// Rounding with a non-default largestUnit to cover the path in
// UnbalanceDurationRelative where larger units are converted into smaller
// units; and with a smallestUnit larger than days to cover the path in
// RoundDuration where days are converted into larger units.
// The calls come from these paths:
// Duration.round() ->
// UnbalanceDurationRelative -> MoveRelativeDate -> calendar.dateAdd()
// RoundDuration -> MoveRelativeDate -> calendar.dateAdd() (2x)
// BalanceDateDurationRelative -> calendar.dateAdd()
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()

calendar.dateAddCallCount = 0;

const instance2 = new Temporal.Duration(0, 1, 1, 1);
instance2.round({ largestUnit: "weeks", smallestUnit: "weeks", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 5, "rounding with non-default largestUnit and calendar smallestUnit");

// Rounding with smallestUnit days only.
// The calls come from these paths:
// Duration.round() ->
// RoundDuration ->
// MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()
// AddZonedDateTime -> calendar.dateAdd()
// DifferenceZonedDateTimeWithRounding ->
// RoundDuration -> MoveRelativeZonedDateTime -> AddZonedDateTime -> calendar.dateAdd()
// BalanceDateDurationRelative -> calendar.dateAdd()

calendar.dateAddCallCount = 0;

const instance3 = new Temporal.Duration(1, 1, 1, 1, 1);
instance3.round({ smallestUnit: "days", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 2, "rounding with days smallestUnit");
instance.round({ smallestUnit: "days", relativeTo });
assert.sameValue(calendar.dateAddCallCount, 3, "rounding with days smallestUnit");
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,9 @@

/*---
esid: sec-temporal.duration.prototype.round
description: The options object passed to calendar.dateUntil has a largestUnit property with its value in the singular form
info: |
sec-temporal.duration.prototype.round steps 23–27:
23. Let _unbalanceResult_ be ? UnbalanceDateDurationRelative(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _largestUnit_, _relativeTo_).
24. Let _roundResult_ be (? RoundDuration(_unbalanceResult_.[[Years]], _unbalanceResult_.[[Months]], _unbalanceResult_.[[Weeks]], _unbalanceResult_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]], _roundingIncrement_, _smallestUnit_, _roundingMode_, _relativeTo_)).[[DurationRecord]].
25. Let _adjustResult_ be ? AdjustRoundedDurationDays(_roundResult_.[[Years]], _roundResult_.[[Months]], _roundResult_.[[Weeks]], _roundResult_.[[Days]], _roundResult_.[[Hours]], _roundResult_.[[Minutes]], _roundResult_.[[Seconds]], _roundResult_.[[Milliseconds]], _roundResult_.[[Microseconds]], _roundResult_.[[Nanoseconds]], _roundingIncrement_, _smallestUnit_, _roundingMode_, _relativeTo_).
26. Let _balanceResult_ be ? BalanceDuration(_adjustResult_.[[Days]], _adjustResult_.[[Hours]], _adjustResult_.[[Minutes]], _adjustResult_.[[Seconds]], _adjustResult_.[[Milliseconds]], _adjustResult_.[[Microseconds]], _adjustResult_.[[Nanoseconds]], _largestUnit_, _relativeTo_).
27. Let _result_ be ? BalanceDurationRelative(_adjustResult_.[[Years]], _adjustResult_.[[Months]], _adjustResult_.[[Weeks]], _balanceResult_.[[Days]], _largestUnit_, _relativeTo_).
sec-temporal-unbalancedatedurationrelative step 3:
3. If _largestUnit_ is *"month"*, then
...
g. Let _untilOptions_ be ! OrdinaryObjectCreate(*null*).
h. Perform ! CreateDataPropertyOrThrow(_untilOptions_, *"largestUnit"*, *"month"*).
i. Let _untilResult_ be ? CalendarDateUntil(_calendarRec_.[[Receiver]], _plainRelativeTo_, _later_, _untilOptions_, _calendarRec_.[[DateUntil]]).
sec-temporal-roundduration steps 5.d and 8.n–p:
5. If _unit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
...
d. Let _result_ be ? NanosecondsToDays(_nanoseconds_, _intermediate_).
...
8. If _unit_ is *"year"*, then
...
n. Let _untilOptions_ be ! OrdinaryObjectCreate(*null*).
o. Perform ! CreateDataPropertyOrThrow(_untilOptions_, *"largestUnit"*, *"year"*).
p. Let _timePassed_ be ? CalendarDateUntil(_calendar_, _relativeTo_, _daysLater_, _untilOptions_)
sec-temporal-adjustroundeddurationdays steps 1 and 9:
1. If _relativeTo_ does not have an [[InitializedTemporalZonedDateTime]] internal slot; or _unit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*; or _unit_ is *"nanosecond"* and _increment_ is 1, then
a. Return ...
...
9. Let _adjustedDateDuration_ be ? AddDuration(_years_, _months_, _weeks_, _days_, 0, 0, 0, 0, 0, 0, 0, 0, 0, _direction_, 0, 0, 0, 0, 0, 0, _relativeTo_).
sec-temporal-addduration step 7.a–g:
a. Assert: _relativeTo_ has an [[IntializedTemporalZonedDateTime]] internal slot.
...
f. If _largestUnit_ is not one of *"year"*, *"month"*, *"week"*, or *"day"*, then
...
g. Else,
i. Let _result_ be ? DifferenceZonedDateTime(_relativeTo_.[[Nanoseconds]], _endNs_, _timeZone_, _calendar_, _largestUnit_).
sec-temporal-balancedurationrelative steps 1, 9.m–o, and 9.q.vi–viii:
1. If _largestUnit_ is not one of *"year"*, *"month"*, or *"week"*, or _years_, _months_, _weeks_, and _days_ are all 0, then
a. Return ...
...
9. If _largestUnit_ is *"year"*, then
...
m. Let _untilOptions_ be ! OrdinaryObjectCreate(*null*).
n. Perform ! CreateDataPropertyOrThrow(_untilOptions_, *"largestUnit"*, *"month"*).
o. Let _untilResult_ be ? CalendarDateUntil(_calendar_, _relativeTo_, _newRelativeTo_, _untilOptions_, _dateUntil_).
p. ...
q. Repeat, while abs(_months_) ≥ abs(_oneYearMonths_),
...
vi. Let _untilOptions_ be ! OrdinaryObjectCreate(*null*).
vii. Perform ! CreateDataPropertyOrThrow(_untilOptions_, *"largestUnit"*, *"month"*).
viii. Let _untilResult_ be ? CalendarDateUntil(_calendar_, _relativeTo_, _newRelativeTo_, _untilOptions_, _dateUntil_).
sec-temporal-balanceduration step 3.a:
3. If _largestUnit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
a. Let _result_ be ? NanosecondsToDays(_nanoseconds_, _relativeTo_).
sec-temporal-differencezoneddatetime steps 7 and 11:
7. Let _dateDifference_ be ? DifferenceISODateTime(_startDateTime_.[[ISOYear]], _startDateTime_.[[ISOMonth]], _startDateTime_.[[ISODay]], _startDateTime_.[[ISOHour]], _startDateTime_.[[ISOMinute]], _startDateTime_.[[ISOSecond]], _startDateTime_.[[ISOMillisecond]], _startDateTime_.[[ISOMicrosecond]], _startDateTime_.[[ISONanosecond]], _endDateTime_.[[ISOYear]], _endDateTime_.[[ISOMonth]], _endDateTime_.[[ISODay]], _endDateTime_.[[ISOHour]], _endDateTime_.[[ISOMinute]], _endDateTime_.[[ISOSecond]], _endDateTime_.[[ISOMillisecond]], _endDateTime_.[[ISOMicrosecond]], _endDateTime_.[[ISONanosecond]], _calendar_, _largestUnit_, _options_).
11. Let _result_ be ? NanosecondsToDays(_timeRemainderNs_, _intermediate_).
sec-temporal-nanosecondstodays step 11:
11. 1. Let _dateDifference_ be ? DifferenceISODateTime(_startDateTime_.[[ISOYear]], _startDateTime_.[[ISOMonth]], _startDateTime_.[[ISODay]], _startDateTime_.[[ISOHour]], _startDateTime_.[[ISOMinute]], _startDateTime_.[[ISOSecond]], _startDateTime_.[[ISOMillisecond]], _startDateTime_.[[ISOMicrosecond]], _startDateTime_.[[ISONanosecond]], _endDateTime_.[[ISOYear]], _endDateTime_.[[ISOMonth]], _endDateTime_.[[ISODay]], _endDateTime_.[[ISOHour]], _endDateTime_.[[ISOMinute]], _endDateTime_.[[ISOSecond]], _endDateTime_.[[ISOMillisecond]], _endDateTime_.[[ISOMicrosecond]], _endDateTime_.[[ISONanosecond]], _relativeTo_.[[Calendar]], *"day"*).
sec-temporal-differenceisodatetime steps 9–11:
9. Let _dateLargestUnit_ be ! LargerOfTwoTemporalUnits(*"day"*, _largestUnit_).
10. Let _untilOptions_ be ? MergeLargestUnitOption(_options_, _dateLargestUnit_).
11. Let _dateDifference_ be ? CalendarDateUntil(_calendar_, _date1_, _date2_, _untilOptions_).
description: >
The options object passed to calendar.dateUntil has a largestUnit property
with its value in the singular form
includes: [compareArray.js, temporalHelpers.js]
features: [Temporal]
---*/
Expand Down Expand Up @@ -99,9 +39,9 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
duration.round({ largestUnit, roundingIncrement: 2, roundingMode: 'ceil', relativeTo });
},
{
years: ["year", "year"],
months: ["month", "month"],
weeks: ["week", "week"],
years: ["year"],
months: ["month"],
weeks: ["week"],
days: [],
hours: [],
minutes: [],
Expand All @@ -123,7 +63,7 @@ TemporalHelpers.checkCalendarDateUntilLargestUnitSingular(
},
{
years: ["year"],
months: ["month", "month"],
months: ["month"],
weeks: ["week"],
days: [],
hours: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,8 @@ features: [Temporal]
// Based on a test case by André Bargull

const calendar = new class extends Temporal.Calendar {
#dateUntil = 0;

dateUntil(one, two, options) {
let result = super.dateUntil(one, two, options);
if (++this.#dateUntil === 2) {
result = result.negated();
}
return result;
return super.dateUntil(one, two, options).negated();
}
}("iso8601");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ const calendar = new CalendarDateUntilObservable("iso8601");
const relativeTo = new Temporal.PlainDate(2018, 10, 12, calendar);

const expected = [
"call dateUntil", // UnbalanceDateDurationRelative
"call dateUntil", // BalanceDateDurationRelative
"call dateUntil", // DifferencePlainDateTimeWithRounding -> DifferenceISODateTime
];

const years = new Temporal.Duration(2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-temporal.duration.prototype.round
description: >
Balancing the resulting duration takes the time zone's UTC offset shifts
into account
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const timeZone = TemporalHelpers.springForwardFallBackTimeZone();

// Based on a test case by Adam Shaw
{
const duration = new Temporal.Duration(1, 0, 0, 0, 24);
const relativeTo = new Temporal.ZonedDateTime(
941184000_000_000_000n /* = 1999-10-29T08Z */,
timeZone); /* = 1999-10-29T00-08 in local time */

const result = duration.round({ largestUnit: "years", relativeTo });
TemporalHelpers.assertDuration(result, 1, 0, 0, 0, 24, 0, 0, 0, 0, 0,
"24 hours does not balance to 1 day in 25-hour day");
}

{
const duration = new Temporal.Duration(0, 0, 0, 0, /* hours = */ 24, 0, 0, 0, 0, /* ns = */ 5);
const relativeTo = new Temporal.ZonedDateTime(
972802800_000_000_000n /* = 2000-10-29T07Z */,
timeZone); /* = 2000-10-29T00-07 in local time */

const result = duration.round({
largestUnit: "days",
smallestUnit: "minutes",
roundingMode: "expand",
roundingIncrement: 30,
relativeTo
});
TemporalHelpers.assertDuration(result, 0, 0, 0, 0, 24, 30, 0, 0, 0, 0,
"24 hours does not balance after rounding to 1 day in 25-hour day");
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();

{
// Date part of duration lands on skipped DST hour, causing disambiguation
const duration = new Temporal.Duration(0, 1, 0, 15, 12);
const duration = new Temporal.Duration(0, 1, 0, 15, 11, 30);
const relativeTo = new Temporal.ZonedDateTime(
950868000_000_000_000n /* = 2000-02-18T10Z */,
timeZone); /* = 2000-02-18T02-08 in local time */
Expand All @@ -29,17 +29,35 @@ const timeZone = TemporalHelpers.springForwardFallBackTimeZone();
}

{
// Month-only part of duration lands on skipped DST hour, should not cause
// disambiguation
const duration = new Temporal.Duration(0, 1, 0, 15);
// Month-only part of duration lands on skipped DST hour
const duration = new Temporal.Duration(0, 1, 0, 15, 0, 30);
const relativeTo = new Temporal.ZonedDateTime(
951991200_000_000_000n /* = 2000-03-02T10Z */,
timeZone); /* = 2000-03-02T02-08 in local time */

TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", relativeTo }),
0, 2, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days should be exactly 1.5 months, which rounds up to 2 months");
"1 month 15 days 00:30 should be exactly 1.5 months, which rounds up to 2 months");
TemporalHelpers.assertDuration(duration.round({ smallestUnit: "months", roundingMode: 'halfTrunc', relativeTo }),
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
"1 month 15 days should be exactly 1.5 months, which rounds down to 1 month");
"1 month 15 days 00:30 should be exactly 1.5 months, which rounds down to 1 month");
}

{
// Day rounding
// DST spring-forward hour skipped at 2000-04-02T02:00 (23 hour day)
// 11.5 hours is 0.5
const duration = new Temporal.Duration(0, 0, 0, 0, 11, 30);
const instant = timeZone.getPossibleInstantsFor(Temporal.PlainDateTime.from("2000-04-02T00:00:00"))[0];
const relativeTo = instant.toZonedDateTimeISO(timeZone);

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "days" }),
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
);

TemporalHelpers.assertDuration(
duration.round({ relativeTo, smallestUnit: "days", roundingMode: "halfTrunc" }),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-temporal.duration.prototype.round
description: Test for all combinations of largestUnit and smallestUnit with relativeTo
includes: [temporalHelpers.js]
features: [Temporal]
---*/

const duration = new Temporal.Duration(5, 5, 5, 5, 5, 5, 5, 5, 5, 5);
const plainRelativeTo = new Temporal.PlainDate(2000, 1, 1);
const zonedRelativeTo = new Temporal.ZonedDateTime(63072000_000_000_000n /* = 1972-01-01T00Z */, "UTC");

const exactResults = {
years: {
years: [6],
months: [5, 6],
weeks: [5, 6, 1],
days: [5, 6, 0, 10],
hours: [5, 6, 0, 10, 5],
minutes: [5, 6, 0, 10, 5, 5],
seconds: [5, 6, 0, 10, 5, 5, 5],
milliseconds: [5, 6, 0, 10, 5, 5, 5, 5],
microseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5],
nanoseconds: [5, 6, 0, 10, 5, 5, 5, 5, 5, 5],
},
months: {
months: [0, 66],
weeks: [0, 66, 1],
days: [0, 66, 0, 10],
hours: [0, 66, 0, 10, 5],
minutes: [0, 66, 0, 10, 5, 5],
seconds: [0, 66, 0, 10, 5, 5, 5],
milliseconds: [0, 66, 0, 10, 5, 5, 5, 5],
microseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5],
nanoseconds: [0, 66, 0, 10, 5, 5, 5, 5, 5, 5],
},
weeks: {
weeks: [0, 0, 288],
days: [0, 0, 288, 2],
hours: [0, 0, 288, 2, 5],
minutes: [0, 0, 288, 2, 5, 5],
seconds: [0, 0, 288, 2, 5, 5, 5],
milliseconds: [0, 0, 288, 2, 5, 5, 5, 5],
microseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5],
nanoseconds: [0, 0, 288, 2, 5, 5, 5, 5, 5, 5],
},
days: {
days: [0, 0, 0, 2018],
hours: [0, 0, 0, 2018, 5],
minutes: [0, 0, 0, 2018, 5, 5],
seconds: [0, 0, 0, 2018, 5, 5, 5],
milliseconds: [0, 0, 0, 2018, 5, 5, 5, 5],
microseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5],
nanoseconds: [0, 0, 0, 2018, 5, 5, 5, 5, 5, 5],
},
hours: {
hours: [0, 0, 0, 0, 48437],
minutes: [0, 0, 0, 0, 48437, 5],
seconds: [0, 0, 0, 0, 48437, 5, 5],
milliseconds: [0, 0, 0, 0, 48437, 5, 5, 5],
microseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5],
nanoseconds: [0, 0, 0, 0, 48437, 5, 5, 5, 5, 5],
},
minutes: {
minutes: [0, 0, 0, 0, 0, 2906225],
seconds: [0, 0, 0, 0, 0, 2906225, 5],
milliseconds: [0, 0, 0, 0, 0, 2906225, 5, 5],
microseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5],
nanoseconds: [0, 0, 0, 0, 0, 2906225, 5, 5, 5, 5],
},
seconds: {
seconds: [0, 0, 0, 0, 0, 0, 174373505],
milliseconds: [0, 0, 0, 0, 0, 0, 174373505, 5],
microseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5],
nanoseconds: [0, 0, 0, 0, 0, 0, 174373505, 5, 5, 5],
},
milliseconds: {
milliseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005],
microseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5],
nanoseconds: [0, 0, 0, 0, 0, 0, 0, 174373505005, 5, 5],
},
microseconds: {
microseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005],
nanoseconds: [0, 0, 0, 0, 0, 0, 0, 0, 174373505005005, 5],
},
};
for (const [largestUnit, entry] of Object.entries(exactResults)) {
for (const [smallestUnit, expected] of Object.entries(entry)) {
for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) {
const [y, mon = 0, w = 0, d = 0, h = 0, min = 0, s = 0, ms = 0, µs = 0, ns = 0] = expected;
TemporalHelpers.assertDuration(
duration.round({ largestUnit, smallestUnit, relativeTo }),
y, mon, w, d, h, min, s, ms, µs, ns,
`Combination of largestUnit ${largestUnit} and smallestUnit ${smallestUnit}, relative to ${relativeTo}`
);
}
}
}

// 174373505005005005 is not a safe integer.
// ℝ(𝔽(174373505005005005)) == 174373505005004992

for (const relativeTo of [plainRelativeTo, zonedRelativeTo]) {
TemporalHelpers.assertDuration(
duration.round({ largestUnit: "nanoseconds", smallestUnit: "nanoseconds", relativeTo }),
0, 0, 0, 0, 0, 0, 0, 0, 0, 174373505005004992,
`Combination of largestUnit nanoseconds and smallestUnit nanoseconds, with precision loss, relative to ${relativeTo}`
);
}
Loading
Loading