Source code
Revision control
Copy as Markdown
Other Tools
// |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl"))
// Test default formatting for Temporal types using different locales and all
// supported calendars.
const locales = [
"en",
"de",
"fr",
"es",
"ar",
"th",
"zh",
"ja",
];
const timeZone = "UTC";
let date = new Date(0);
let instant = date.toTemporalInstant();
let zonedDateTime = instant.toZonedDateTimeISO(timeZone);
let plainDateTime = zonedDateTime.toPlainDateTime();
let plainDate = zonedDateTime.toPlainDate();
let plainTime = zonedDateTime.toPlainTime();
for (let locale of locales) {
for (let calendar of Intl.supportedValuesOf("calendar")) {
// Calendar must match for YearMonth and MonthDay.
//
let calendarDate = plainDate.withCalendar(calendar);
let calendarYearMonth = calendarDate.toPlainYearMonth();
let calendarMonthDay = calendarDate.toPlainMonthDay();
assertEq(
instant.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleString(locale, {timeZone, calendar})
);
assertEq(
zonedDateTime.toLocaleString(locale, {calendar}),
date.toLocaleString(locale, {timeZone, calendar, timeZoneName: "short"})
);
assertEq(
plainDateTime.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleString(locale, {timeZone, calendar})
);
assertEq(
plainDate.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleDateString(locale, {timeZone, calendar})
);
assertEq(
plainTime.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleTimeString(locale, {timeZone, calendar})
);
assertEq(
calendarYearMonth.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleDateString(locale, {timeZone, calendar, year: "numeric", month: "numeric"})
);
// ICU4X and ICU4C don't agree on calendar computations for islamic-umalqura.
//
//
// ICU4X and ICU4C are possibly both wrong for dates around 1970 when
// comparing to these comparison charts:
//
// Also see:
if (calendar !== "islamic-umalqura") {
assertEq(
calendarMonthDay.toLocaleString(locale, {timeZone, calendar}),
date.toLocaleDateString(locale, {timeZone, calendar, month: "numeric", day: "numeric"}),
);
}
}
}
if (typeof reportCompare === "function")
reportCompare(true, true);