Skip to content

Commit

Permalink
Temporal: Tests for round() and since()/until() using the same code path
Browse files Browse the repository at this point in the history
This should produce all the same results (except for a change to weeks
balancing in round(), which is now more consistent with since()/until())
but leads to different observable user code calls.

See tc39/proposal-temporal#2742
  • Loading branch information
ptomato authored and Ms2ger committed May 14, 2024
1 parent 6c6c72b commit 96e31e7
Show file tree
Hide file tree
Showing 54 changed files with 409 additions and 1,599 deletions.
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 });
assert.sameValue(calendar.dateAddCallCount, 4, "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, 6, "rounding with non-default largestUnit and calendar smallestUnit");
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 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 });
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
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,
);
}

This file was deleted.

0 comments on commit 96e31e7

Please sign in to comment.