-
Notifications
You must be signed in to change notification settings - Fork 508
DRAFT: Temporal: Add test for formatting Temporal objects with era formatter #4634
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
Draft
catamorphism
wants to merge
6
commits into
tc39:main
Choose a base branch
from
catamorphism:issue-3049
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bb83497
Temporal: Add test for formatting Temporal objects with era formatter
catamorphism cf569de
Be consistent with spec about not including era when formatting a Pla…
catamorphism 561c671
Add more tests
catamorphism 8ae6c62
Fix locales
catamorphism 28e653d
Pass explicit time zones
catamorphism bd540ad
Fix locales again
catamorphism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/Instant/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.instant.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| Instant should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const instant = new Temporal.Instant(0n); | ||
|
|
||
| assert(instant.toLocaleString("en", { era: "narrow", timeZone: "UTC" }).startsWith("1"), "toLocaleString on an Instant with era option should work"); |
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/PlainDate/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.plaindate.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| PlainDate should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const date = new Temporal.PlainDate(2000, 5, 2, "gregory"); | ||
|
|
||
| assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainDate with era option should work"); |
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/PlainDateTime/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.plaindatetime.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| PlainDateTime should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| date = new Temporal.PlainDateTime(2000, 5, 2, 14, 46, 0, 0, 0, 0, "gregory"); | ||
|
|
||
| assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainDateTime with era option should work"); |
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/PlainMonthDay/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.plainmonthday.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| PlainMonthDay should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const date = new Temporal.PlainMonthDay(5, 2, "gregory"); | ||
|
|
||
| assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainMonthDay with era option should work"); |
18 changes: 18 additions & 0 deletions
18
test/built-ins/Temporal/PlainTime/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // 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.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| PlainTime should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const date = new Temporal.PlainTime(14, 46); | ||
|
|
||
| const result = date.toLocaleString("en", { era: "narrow" }); | ||
|
|
||
| assert(result.startsWith("2"), "toLocaleString on a PlainTime with era option should work"); | ||
| assert(!result.includes("A"), "era should be ignored when formatting a PlainTime"); |
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/PlainYearMonth/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.plainyearmonth.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| PlainYearMonth should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const date = new Temporal.PlainYearMonth(2000, 5, "gregory"); | ||
|
|
||
| assert(date.toLocaleString("en", { era: "narrow" }).startsWith("5"), "toLocaleString on a PlainYearMonth with era option should work"); |
15 changes: 15 additions & 0 deletions
15
test/built-ins/Temporal/ZonedDateTime/prototype/toLocaleString/era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // 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.zoneddatetime.prototype.tolocalestring | ||
| description: > | ||
| If era option and no other options are provided to toLocaleString, | ||
| ZonedDateTime should be foramtted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const zdt = new Temporal.ZonedDateTime(0n, "UTC"); | ||
|
|
||
| assert(zdt.toLocaleString("en", { era: "narrow" }).startsWith("1"), "toLocaleString on a ZonedDateTime with era option should work"); |
20 changes: 20 additions & 0 deletions
20
test/intl402/DateTimeFormat/prototype/format/temporal-objects-format-with-era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (C) 2024 Igalia, S.L. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-datetime-format-functions | ||
| description: > | ||
| If era option and no other options are provided to DateTimeFormat constructor, | ||
| objects should be formatted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow", timeZone: "UTC" }); | ||
|
|
||
| assert(formatter.format(new Temporal.PlainDate(2025, 11, 4)).startsWith("11"), "formatting a PlainDate should work"); | ||
| assert(formatter.format(new Temporal.PlainYearMonth(2025, 11, "gregory")).startsWith("11"), "formatting a PlainYearMonth should work"); | ||
| assert(formatter.format(new Temporal.PlainMonthDay(11, 4, "gregory")).startsWith("11"), "formatting a PlainMonthDay should work"); | ||
| assert(formatter.format(new Temporal.PlainTime(14, 46)).startsWith("2"), "formatting a PlainTime should work"); | ||
| assert(formatter.format(new Temporal.PlainDateTime(2025, 11, 4, 14, 46)).startsWith("11"), "formatting a PlainDateTime should work"); | ||
| assert(formatter.format(new Temporal.Instant(0n)).startsWith("1"), "formatting an Instant should work"); | ||
20 changes: 20 additions & 0 deletions
20
test/intl402/DateTimeFormat/prototype/formatRange/temporal-objects-format-with-era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright (C) 2024 Igalia, S.L. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-datetime-format-functions | ||
| description: > | ||
| If era option and no other options are provided to DateTimeFormat constructor, | ||
| objects should be formatted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow", timeZone: "UTC" }); | ||
|
|
||
| assert(formatter.format(new Temporal.PlainDate(2025, 11, 4)).startsWith("11"), "formatting a PlainDate should work"); | ||
| assert(formatter.format(new Temporal.PlainYearMonth(2025, 11, "gregory")).startsWith("11"), "formatting a PlainYearMonth should work"); | ||
| assert(formatter.format(new Temporal.PlainMonthDay(11, 4, "gregory")).startsWith("11"), "formatting a PlainMonthDay should work"); | ||
| assert(formatter.format(new Temporal.PlainTime(14, 46)).startsWith("2"), "formatting a PlainTime should work"); | ||
| assert(formatter.format(new Temporal.PlainDateTime(2025, 11, 4, 14, 46)).startsWith("11"), "formatting a PlainDateTime should work"); | ||
| assert(formatter.format(new Temporal.Instant(0n)).startsWith("1"), "formatting an Instant should work"); |
29 changes: 29 additions & 0 deletions
29
test/intl402/DateTimeFormat/prototype/formatRangeToParts/temporal-objects-format-with-era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (C) 2024 Igalia, S.L. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-datetime-format-functions | ||
| description: > | ||
| If era option and no other options are provided to DateTimeFormat constructor, | ||
| objects should be formatted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow", timeZone: "UTC" }); | ||
|
|
||
| function checkEra(parts) { | ||
| for (let part of parts) { | ||
| if (part.type === 'era' && part.value.startsWith('A')) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainDate(2025, 11, 4), new Temporal.PlainDate(2025, 11, 5))), "formatting a PlainDate should work"); | ||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainDate(2025, 11, 4), new Temporal.PlainDate(2025, 11, 5))), "formatting a PlainDate should work"); | ||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainYearMonth(2025, 11, "gregory"), new Temporal.PlainYearMonth(2025, 12, "gregory"))), "formatting a PlainYearMonth should work"); | ||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainMonthDay(11, 4, "gregory"), new Temporal.PlainMonthDay(11, 14, "gregory"))), "formatting a PlainMonthDay should work"); | ||
| assert(!checkEra(formatter.formatRangeToParts(new Temporal.PlainTime(14, 46), new Temporal.PlainTime(17, 46))), "formatting a PlainTime should work"); | ||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.PlainDateTime(2025, 11, 4, 14, 46), new Temporal.PlainDateTime(2025, 11, 15, 14, 47))), "formatting a PlainDateTime should work"); | ||
| assert(checkEra(formatter.formatRangeToParts(new Temporal.Instant(0n), new Temporal.Instant(1000000000n))), "formatting an Instant should work"); |
28 changes: 28 additions & 0 deletions
28
test/intl402/DateTimeFormat/prototype/formatToParts/temporal-objects-format-with-era.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (C) 2025 Igalia, S.L. All rights reserved. | ||
| // This code is governed by the BSD license found in the LICENSE file. | ||
|
|
||
| /*--- | ||
| esid: sec-datetime-format-functions | ||
| description: > | ||
| If era option and no other options are provided to DateTimeFormat constructor, | ||
| objects should be formatted with default options | ||
| features: [Temporal] | ||
| locale: [en] | ||
| ---*/ | ||
|
|
||
| const formatter = new Intl.DateTimeFormat(["en"], { era: "narrow", timeZone: "UTC" }); | ||
|
|
||
| function checkEra(parts) { | ||
| for (let part of parts) { | ||
| if (part.type === 'era' && part.value.startsWith('A')) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| assert(checkEra(formatter.formatToParts(new Temporal.PlainDate(2025, 11, 4))), "formatting a PlainDate should work"); | ||
| assert(checkEra(formatter.formatToParts(new Temporal.PlainYearMonth(2025, 11, "gregory"))), "formatting a PlainYearMonth should work"); | ||
| assert(checkEra(formatter.formatToParts(new Temporal.PlainMonthDay(11, 4, "gregory"))), "formatting a PlainMonthDay should work"); | ||
| assert(!checkEra(formatter.formatToParts(new Temporal.PlainTime(14, 46))), "formatting a PlainTime should work"); | ||
| assert(checkEra(formatter.formatToParts(new Temporal.PlainDateTime(2025, 11, 4, 14, 46))), "formatting a PlainDateTime should work"); | ||
| assert(checkEra(formatter.formatToParts(new Temporal.Instant(0n))), "formatting an Instant should work"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
"gregory"may fail depending on the calendar thatformattergets from the host's locale. Probably the best way to work around this would be to hardcode the formatter to"en", and includelocales: [en]in the frontmatter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 8ae6c62
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/locales/locale apparently. Sorry!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, it looks like the linter complains about including
locales: [en]in the frontmatter? Or did I do that incorrectly?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got it wrong, it's
localehttps://github.com/tc39/test262/blob/main/CONTRIBUTING.md#localeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in bd540ad