Skip to content
Draft
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
15 changes: 15 additions & 0 deletions test/built-ins/Temporal/Instant/prototype/toLocaleString/era.js
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");
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");
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");
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");
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");
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");
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");
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");
Comment on lines +16 to +17
Copy link
Contributor

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 that formatter gets from the host's locale. Probably the best way to work around this would be to hardcode the formatter to "en", and include locales: [en] in the frontmatter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8ae6c62

Copy link
Contributor

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!

Copy link
Contributor Author

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?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in bd540ad

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");
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");
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");
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");
Loading