Skip to content

Commit 4aa9379

Browse files
anbaptomato
authored andcommitted
Temporal: Coverage for Temporal.ZonedDateTime
1 parent 4ceee16 commit 4aa9379

15 files changed

+580
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.from
6+
description: >
7+
Start-of-day is outside the valid epoch nanoseconds limits.
8+
info: |
9+
Temporal.ZonedDateTime.from ( item [ , options ] )
10+
11+
1. Return ? ToTemporalZonedDateTime(item, options).
12+
13+
ToTemporalZonedDateTime ( item [ , options ] )
14+
15+
...
16+
8. Let epochNanoseconds be ? InterpretISODateTimeOffset(isoDate, time,
17+
offsetBehaviour, offsetNanoseconds, timeZone, disambiguation, offsetOption,
18+
matchBehaviour).
19+
...
20+
21+
InterpretISODateTimeOffset ( isoDate, time, offsetBehaviour, offsetNanoseconds,
22+
timeZone, disambiguation, offsetOption, matchBehaviour )
23+
24+
1. If time is start-of-day, then
25+
a. Assert: offsetBehaviour is wall.
26+
b. Assert: offsetNanoseconds is 0.
27+
c. Return ? GetStartOfDay(timeZone, isoDate).
28+
...
29+
features: [Temporal]
30+
---*/
31+
32+
assert.throws(
33+
RangeError,
34+
() => Temporal.ZonedDateTime.from("-271821-04-20[+01]"),
35+
"From '-271821-04-20[+01]'"
36+
);
37+
38+
assert.throws(
39+
RangeError,
40+
() => Temporal.ZonedDateTime.from("+275760-09-13[-01]"),
41+
"From '+275760-09-13[-01]'"
42+
);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime
6+
description: >
7+
OrdinaryCreateFromConstructor returns with an abrupt completion.
8+
info: |
9+
CreateTemporalZonedDateTime ( epochNanoseconds, timeZone, calendar [ , newTarget ] )
10+
11+
...
12+
3. Let object be ? OrdinaryCreateFromConstructor(newTarget,
13+
"%Temporal.ZonedDateTime.prototype%", « [[InitializedTemporalZonedDateTime]],
14+
[[EpochNanoseconds]], [[TimeZone]], [[Calendar]] »).
15+
...
16+
17+
OrdinaryCreateFromConstructor ( constructor, intrinsicDefaultProto [ , internalSlotsList ] )
18+
19+
...
20+
2. Let proto be ? GetPrototypeFromConstructor(constructor, intrinsicDefaultProto).
21+
...
22+
23+
GetPrototypeFromConstructor ( constructor, intrinsicDefaultProto )
24+
25+
...
26+
2. Let proto be ? Get(constructor, "prototype").
27+
...
28+
29+
features: [Temporal]
30+
---*/
31+
32+
var newTarget = Object.defineProperty(function(){}.bind(), "prototype", {
33+
get() {
34+
throw new Test262Error();
35+
}
36+
});
37+
38+
assert.throws(Test262Error, function() {
39+
Reflect.construct(Temporal.ZonedDateTime, [0n, "UTC"], newTarget)
40+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime
6+
description: >
7+
RangeError thrown when epoch nanoseconds not valid.
8+
info: |
9+
Temporal.ZonedDateTime ( epochNanoseconds, timeZone [ , calendar ] )
10+
11+
2. Set epochNanoseconds to ? ToBigInt(epochNanoseconds).
12+
3. If IsValidEpochNanoseconds(epochNanoseconds) is false, throw a RangeError exception.
13+
...
14+
features: [Temporal]
15+
---*/
16+
17+
var nsMaxInstant = 864n * 10n ** 19n;
18+
var nsMinInstant = -nsMaxInstant;
19+
20+
var invalidEpochNanoseconds = [
21+
nsMaxInstant + 1n,
22+
nsMinInstant - 1n,
23+
2n ** 128n,
24+
-(2n ** 128n),
25+
];
26+
27+
var timeZones = [
28+
"UTC",
29+
"+00",
30+
"+01",
31+
"-01",
32+
];
33+
34+
for (var timeZone of timeZones) {
35+
for (var epochNs of invalidEpochNanoseconds) {
36+
assert.throws(
37+
RangeError,
38+
() => new Temporal.ZonedDateTime(epochNs, timeZone),
39+
`epochNs = ${epochNs}, timeZone = ${timeZone}`
40+
);
41+
}
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.add
6+
description: >
7+
Throws RangeError when intermediate date-time is outside valid limits.
8+
info: |
9+
Temporal.ZonedDateTime.prototype.add ( temporalDurationLike [ , options ] )
10+
11+
...
12+
3. Return ? AddDurationToZonedDateTime(add, zonedDateTime, temporalDurationLike, options).
13+
14+
AddDurationToZonedDateTime ( operation, zonedDateTime, temporalDurationLike, options )
15+
16+
...
17+
8. Let epochNanoseconds be ? AddZonedDateTime(zonedDateTime.[[EpochNanoseconds]], timeZone, calendar, internalDuration, overflow).
18+
...
19+
20+
AddZonedDateTime ( epochNanoseconds, timeZone, calendar, duration, overflow )
21+
22+
...
23+
4. Let intermediateDateTime be CombineISODateAndTimeRecord(addedDate, isoDateTime.[[Time]]).
24+
5. If ISODateTimeWithinLimits(intermediateDateTime) is false, throw a RangeError exception.
25+
...
26+
features: [Temporal]
27+
---*/
28+
29+
var nsMaxInstant = 864n * 10n**19n;
30+
var nsMinInstant = -nsMaxInstant;
31+
32+
var epochNs = nsMinInstant;
33+
var zdt = new Temporal.ZonedDateTime(epochNs, "UTC");
34+
35+
assert.throws(RangeError, () => zdt.add({days: -1}));
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.hoursinday
6+
description: >
7+
Basic tests for hoursInDay.
8+
features: [Temporal]
9+
---*/
10+
11+
var nsPerDay = 24n * 60n * 60n * 1000n * 1000n * 1000n;
12+
13+
var epochNanoseconds = [
14+
0n,
15+
nsPerDay,
16+
-nsPerDay,
17+
];
18+
19+
var timeZones = [
20+
"UTC",
21+
"+00",
22+
"+01",
23+
"-01",
24+
];
25+
26+
for (var timeZone of timeZones) {
27+
for (var epochNs of epochNanoseconds) {
28+
var zdt = new Temporal.ZonedDateTime(epochNs, timeZone);
29+
assert.sameValue(zdt.hoursInDay, 24, `epochNs = ${epochNs}, timeZone = ${timeZone}`);
30+
}
31+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.hoursinday
6+
description: >
7+
GetStartOfDay throws a RangeError for values outside the valid limits.
8+
info: |
9+
get Temporal.ZonedDateTime.prototype.hoursInDay
10+
11+
...
12+
7. Let todayNs be ? GetStartOfDay(timeZone, today).
13+
8. Let tomorrowNs be ? GetStartOfDay(timeZone, tomorrow).
14+
...
15+
features: [Temporal]
16+
---*/
17+
18+
var zdt;
19+
20+
// GetStartOfDay for |today| fails.
21+
zdt = new Temporal.ZonedDateTime(-864n * 10n**19n, "-01");
22+
assert.throws(RangeError, () => zdt.hoursInDay);
23+
24+
// GetStartOfDay for |today| fails.
25+
zdt = new Temporal.ZonedDateTime(-864n * 10n**19n, "+01");
26+
assert.throws(RangeError, () => zdt.hoursInDay);
27+
28+
// GetStartOfDay for |tomorrow| fails.
29+
zdt = new Temporal.ZonedDateTime(864n * 10n**19n, "-01");
30+
assert.throws(RangeError, () => zdt.hoursInDay);
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.round
6+
description: >
7+
GetStartOfDay throws a RangeError for values outside the valid limits.
8+
info: |
9+
Temporal.ZonedDateTime.prototype.round ( roundTo )
10+
11+
...
12+
18. If smallestUnit is day, then
13+
...
14+
c. Let startNs be ? GetStartOfDay(timeZone, dateStart).
15+
...
16+
e. Let endNs be ? GetStartOfDay(timeZone, dateEnd).
17+
...
18+
features: [Temporal]
19+
---*/
20+
21+
var roundTo = {smallestUnit: "days"};
22+
23+
var zdt;
24+
25+
// GetStartOfDay for |dateStart| fails.
26+
zdt = new Temporal.ZonedDateTime(-864n * 10n**19n, "-01");
27+
assert.throws(RangeError, () => zdt.round(roundTo));
28+
29+
// GetStartOfDay for |dateStart| fails.
30+
zdt = new Temporal.ZonedDateTime(-864n * 10n**19n, "+01");
31+
assert.throws(RangeError, () => zdt.round(roundTo));
32+
33+
// GetStartOfDay for |dateEnd| fails.
34+
zdt = new Temporal.ZonedDateTime(864n * 10n**19n, "-01");
35+
assert.throws(RangeError, () => zdt.round(roundTo));
36+
37+
// GetStartOfDay for |dateEnd| fails.
38+
zdt = new Temporal.ZonedDateTime(864n * 10n**19n, "+00");
39+
assert.throws(RangeError, () => zdt.round(roundTo));
40+
41+
// GetStartOfDay for |dateEnd| fails.
42+
zdt = new Temporal.ZonedDateTime(864n * 10n**19n, "+01");
43+
assert.throws(RangeError, () => zdt.round(roundTo));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.round
6+
description: >
7+
Throws RangeError when rounded ISO date-time is outside the valid limits.
8+
info: |
9+
Temporal.ZonedDateTime.prototype.round ( roundTo )
10+
11+
...
12+
18. If smallestUnit is day, then
13+
...
14+
19. Else,
15+
a. Let roundResult be RoundISODateTime(isoDateTime, roundingIncrement,
16+
smallestUnit, roundingMode).
17+
...
18+
c. Let epochNanoseconds be ? InterpretISODateTimeOffset(roundResult.[[ISODate]],
19+
roundResult.[[Time]], option, offsetNanoseconds, timeZone, compatible, prefer,
20+
match-exactly).
21+
...
22+
features: [Temporal]
23+
---*/
24+
25+
var nsMaxInstant = 864n * 10n**19n;
26+
27+
var epochNs = nsMaxInstant;
28+
var zdt = new Temporal.ZonedDateTime(epochNs, "+23:59");
29+
30+
var roundTo = {
31+
smallestUnit: "minutes",
32+
roundingIncrement: 10,
33+
roundingMode: "ceil",
34+
};
35+
36+
// |isoDateTime| is +275760-09-13T23:59.
37+
// |roundResult| is +275760-09-14T00:00, which is outside the valid limits.
38+
assert.throws(RangeError, () => zdt.round(roundTo));
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright (C) 2024 André Bargull. All rights reserved.
2+
// This code is governed by the BSD license found in the LICENSE file.
3+
4+
/*---
5+
esid: sec-temporal.zoneddatetime.prototype.since
6+
description: >
7+
Returns a blank duration when epoch nanoseconds are equal.
8+
info: |
9+
Temporal.ZonedDateTime.prototype.since ( other [ , options ] )
10+
11+
3. Return ? DifferenceTemporalZonedDateTime(since, zonedDateTime, other, options).
12+
13+
DifferenceTemporalZonedDateTime ( operation, zonedDateTime, other, options )
14+
15+
...
16+
8. If zonedDateTime.[[EpochNanoseconds]] = other.[[EpochNanoseconds]], then
17+
a. Return ! CreateTemporalDuration(0, 0, 0, 0, 0, 0, 0, 0, 0, 0).
18+
...
19+
includes: [temporalHelpers.js]
20+
features: [Temporal]
21+
---*/
22+
23+
var epochNanoseconds = [
24+
0n,
25+
1n,
26+
-1n,
27+
];
28+
29+
var timeZones = [
30+
"UTC",
31+
"+00",
32+
"+01",
33+
"-01",
34+
];
35+
36+
var units = [
37+
"years",
38+
"months",
39+
"weeks",
40+
"days",
41+
"hours",
42+
"minutes",
43+
"seconds",
44+
"milliseconds",
45+
"microseconds",
46+
"nanoseconds",
47+
];
48+
49+
for (var timeZone of timeZones) {
50+
for (var epochNs of epochNanoseconds) {
51+
var zdt = new Temporal.ZonedDateTime(epochNs, timeZone);
52+
var other = new Temporal.ZonedDateTime(epochNs, timeZone);
53+
54+
for (var i = 0; i < units.length; ++i) {
55+
for (var j = i; j < units.length; ++j) {
56+
var options = {
57+
largestUnit: units[i],
58+
smallestUnit: units[j],
59+
};
60+
61+
TemporalHelpers.assertDuration(
62+
zdt.since(other, options),
63+
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64+
`epochNs = ${epochNs}, timeZone = ${timeZone}, options = ${JSON.stringify(options)})`
65+
);
66+
}
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)