Skip to content

Commit c9315a4

Browse files
committed
Temporal: Add additional tests for TimeClip in DateTimeFormat methods
Same test as tc39#4940, but for the formatRange/formatToParts/formatRangeToParts methods. I noticed in my polyfill implementation that it's possible to pass the tests in tc39#4940 while still implementing the other methods incorrectly.
1 parent f573178 commit c9315a4

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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-partitiondatetimerangepattern
6+
description: TimeClip is not applied for Temporal plain objects.
7+
info: |
8+
PartitionDateTimeRangePattern ( _dateTimeFormat_, _x_, _y_ )
9+
2. Let _xFormatRecord_ be ? ValueFormatRecord(_dateTimeFormat_, _x_).
10+
3. Let _yFormatRecord_ be ? ValueFormatRecord(_dateTimeFormat_, _y_).
11+
features: [Temporal]
12+
locale: [en]
13+
---*/
14+
15+
// Based on the test in
16+
// DateTime/prototype/format/temporal-objects-no-time-clip.js by André Bargull.
17+
18+
// Test with Temporal default calendar "iso8601" and additionally "gregory".
19+
var calendars = ["iso8601", "gregory"];
20+
21+
for (var calendar of calendars) {
22+
var dtf = new Intl.DateTimeFormat("en", {calendar});
23+
24+
// Minimum and maximum PlainDate values
25+
var minDate = new Temporal.PlainDate(-271821, 4, 19, calendar);
26+
var maxDate = new Temporal.PlainDate(275760, 9, 13, calendar);
27+
var dateResult = dtf.formatRange(minDate, maxDate);
28+
assert(dateResult.includes("-271821") || dateResult.includes("271822"), "dateResult includes min year");
29+
assert(dateResult.includes("4"), "dateResult includes min month");
30+
assert(dateResult.includes("19"), "dateResult includes min day");
31+
assert(dateResult.includes("275760"), "dateResult includes max year");
32+
// no point in asserting it includes month "9", since it already includes "19"
33+
assert(dateResult.includes("13"), "result includes max day");
34+
35+
// Minimum and maximum PlainDateTime values
36+
var minDateTime = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1, calendar);
37+
var maxDateTime = new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999, calendar);
38+
var dateTimeResult = dtf.formatRange(minDateTime, maxDateTime);
39+
assert(dateTimeResult.includes("-271821") || dateTimeResult.includes("271822"), "dateTimeResult includes min year");
40+
assert(dateTimeResult.includes("4"), "dateTimeResult includes min month");
41+
assert(dateTimeResult.includes("19"), "dateTimeResult includes min day");
42+
assert(dateTimeResult.includes("275760"), "dateTimeResult includes max year");
43+
assert(dateTimeResult.includes("13"), "dateTimeResult includes max day");
44+
45+
// Minimum and maximum PlainYearMonth values
46+
var minYearMonth = new Temporal.PlainYearMonth(-271821, 4, calendar);
47+
var maxYearMonth = new Temporal.PlainYearMonth(275760, 9, calendar);
48+
var yearMonthResult = dtf.formatRange(minYearMonth, maxYearMonth);
49+
assert(yearMonthResult.includes("-271821") || yearMonthResult.includes("271822"), "yearMonthResult includes min year");
50+
assert(yearMonthResult.includes("4"), "yearMonthResult includes min month");
51+
assert(yearMonthResult.includes("275760"), "yearMonthResult includes max year");
52+
assert(yearMonthResult.includes("9"), "yearMonthResult includes max month");
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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-partitiondatetimerangepattern
6+
description: TimeClip is not applied for Temporal plain objects.
7+
info: |
8+
PartitionDateTimeRangePattern ( _dateTimeFormat_, _x_, _y_ )
9+
2. Let _xFormatRecord_ be ? ValueFormatRecord(_dateTimeFormat_, _x_).
10+
3. Let _yFormatRecord_ be ? ValueFormatRecord(_dateTimeFormat_, _y_).
11+
features: [Temporal]
12+
locale: [en]
13+
---*/
14+
15+
// Based on the test in
16+
// DateTime/prototype/format/temporal-objects-no-time-clip.js by André Bargull.
17+
18+
function findPart(parts, expectedType, expectedSource) {
19+
return parts.find(({ type, source }) => type === expectedType && source === expectedSource).value;
20+
}
21+
22+
// Test with Temporal default calendar "iso8601" and additionally "gregory".
23+
var calendars = ["iso8601", "gregory"];
24+
25+
for (var calendar of calendars) {
26+
var dtf = new Intl.DateTimeFormat("en", {calendar});
27+
28+
// Minimum and maximum PlainDate values
29+
var minDate = new Temporal.PlainDate(-271821, 4, 19, calendar);
30+
var maxDate = new Temporal.PlainDate(275760, 9, 13, calendar);
31+
var dateParts = dtf.formatRangeToParts(minDate, maxDate);
32+
var yearPart = +findPart(dateParts, "year", "startRange");
33+
assert(yearPart === -271821 || yearPart === 271822, "dateParts includes min year");
34+
assert.sameValue(+findPart(dateParts, "month", "startRange"), 4, "dateParts includes min month");
35+
assert.sameValue(+findPart(dateParts, "day", "startRange"), 19, "dateParts includes min day");
36+
assert.sameValue(+findPart(dateParts, "year", "endRange"), 275760, "dateParts includes max year");
37+
assert.sameValue(+findPart(dateParts, "month", "endRange"), 9, "dateParts includes max month");
38+
assert.sameValue(+findPart(dateParts, "day", "endRange"), 13, "dateParts includes max day");
39+
40+
// Minimum and maximum PlainDateTime values
41+
var minDateTime = new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1, calendar);
42+
var maxDateTime = new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999, calendar);
43+
var dateTimeParts = dtf.formatRangeToParts(minDateTime, maxDateTime);
44+
yearPart = +findPart(dateTimeParts, "year", "startRange");
45+
assert(yearPart === -271821 || yearPart === 271822, "dateTimeParts includes min year");
46+
assert.sameValue(+findPart(dateTimeParts, "month", "startRange"), 4, "dateTimeParts includes min month");
47+
assert.sameValue(+findPart(dateTimeParts, "day", "startRange"), 19, "dateTimeParts includes min day");
48+
assert.sameValue(+findPart(dateTimeParts, "year", "endRange"), 275760, "dateTimeParts includes max year");
49+
assert.sameValue(+findPart(dateTimeParts, "month", "endRange"), 9, "dateTimeParts includes max month");
50+
assert.sameValue(+findPart(dateTimeParts, "day", "endRange"), 13, "dateTimeParts includes max day");
51+
52+
// Minimum and maximum PlainYearMonth values
53+
var minYearMonth = new Temporal.PlainYearMonth(-271821, 4, calendar);
54+
var maxYearMonth = new Temporal.PlainYearMonth(275760, 9, calendar);
55+
var yearMonthParts = dtf.formatRangeToParts(minYearMonth, maxYearMonth);
56+
yearPart = +findPart(yearMonthParts, "year", "startRange");
57+
assert(yearPart === -271821 || yearPart === 271822, "yearMonthParts includes min year");
58+
assert.sameValue(+findPart(yearMonthParts, "month", "startRange"), 4, "yearMonthParts includes min month");
59+
assert.sameValue(+findPart(yearMonthParts, "year", "endRange"), 275760, "yearMonthParts includes max year");
60+
assert.sameValue(+findPart(yearMonthParts, "month", "endRange"), 9, "yearMonthParts includes max month");
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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-partitiondatetimepattern
6+
description: TimeClip is not applied for Temporal plain objects.
7+
info: |
8+
PartitionDateTimePattern ( _dateTimeFormat_, _x_ )
9+
1. Let _formatRecord_ be ? ValueFormatRecord(_dateTimeFormat_, _x_).
10+
features: [Temporal]
11+
locale: [en]
12+
---*/
13+
14+
// Based on the test in
15+
// DateTime/prototype/format/temporal-objects-no-time-clip.js by André Bargull.
16+
17+
function findPart(parts, expectedType) {
18+
return parts.find(({ type }) => type === expectedType).value;
19+
}
20+
21+
// Test with Temporal default calendar "iso8601" and additionally "gregory".
22+
var calendars = ["iso8601", "gregory"];
23+
24+
for (var calendar of calendars) {
25+
var dtf = new Intl.DateTimeFormat("en", {calendar});
26+
27+
// Minimum plain date value.
28+
var minDate = dtf.formatToParts(new Temporal.PlainDate(-271821, 4, 19, calendar));
29+
let yearPart = +findPart(minDate, "year");
30+
assert(yearPart === -271821 || yearPart === 271822, "minDate includes year");
31+
assert.sameValue(+findPart(minDate, "month"), 4, "minDate includes month");
32+
assert.sameValue(+findPart(minDate, "day"), 19, "minDate includes day");
33+
34+
// Maximum plain date value.
35+
var maxDate = dtf.formatToParts(new Temporal.PlainDate(275760, 9, 13, calendar));
36+
assert.sameValue(+findPart(maxDate, "year"), 275760, "maxDate includes year");
37+
assert.sameValue(+findPart(maxDate, "month"), 9, "maxDate includes month");
38+
assert.sameValue(+findPart(maxDate, "day"), 13, "maxDate includes day");
39+
40+
// Minimum plain date-time value.
41+
var minDateTime = dtf.formatToParts(new Temporal.PlainDateTime(-271821, 4, 19, 0, 0, 0, 0, 0, 1, calendar));
42+
yearPart = +findPart(minDateTime, "year");
43+
assert(yearPart === -271821 || yearPart === 271822, "minDateTime includes year");
44+
assert.sameValue(+findPart(minDateTime, "month"), 4, "minDateTime includes month");
45+
assert.sameValue(+findPart(minDateTime, "day"), 19, "minDateTime includes day");
46+
47+
// Maximum plain date-time value.
48+
var maxDateTime = dtf.formatToParts(new Temporal.PlainDateTime(275760, 9, 13, 23, 59, 59, 999, 999, 999, calendar));
49+
assert.sameValue(+findPart(maxDateTime, "year"), 275760, "maxDateTime includes year");
50+
assert.sameValue(+findPart(maxDateTime, "month"), 9, "maxDateTime includes month");
51+
assert.sameValue(+findPart(maxDateTime, "day"), 13, "maxDateTime includes day");
52+
53+
// Minimum plain year-month value.
54+
var minYearMonth = dtf.formatToParts(new Temporal.PlainYearMonth(-271821, 4, calendar));
55+
yearPart = +findPart(minYearMonth, "year");
56+
assert(yearPart === -271821 || yearPart === 271822, "minYearMonth includes year");
57+
assert.sameValue(+findPart(minYearMonth, "month"), 4, "minYearMonth includes month");
58+
59+
// Maximum plain year-month value.
60+
var maxYearMonth = dtf.formatToParts(new Temporal.PlainYearMonth(275760, 9, calendar));
61+
assert.sameValue(+findPart(maxYearMonth, "year"), 275760, "maxYearMonth includes year");
62+
assert.sameValue(+findPart(maxYearMonth, "month"), 9, "maxYearMonth includes month");
63+
}

0 commit comments

Comments
 (0)