| 1 |
// |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl")) |
| 2 |
|
| 3 |
// Test default formatting for Temporal types using different locales and all |
| 4 |
// supported calendars. |
| 5 |
|
| 6 |
const locales = [ |
| 7 |
"en", |
| 8 |
"de", |
| 9 |
"fr", |
| 10 |
"es", |
| 11 |
"ar", |
| 12 |
"th", |
| 13 |
"zh", |
| 14 |
"ja", |
| 15 |
]; |
| 16 |
|
| 17 |
const timeZone = "UTC"; |
| 18 |
|
| 19 |
let date = new Date(0); |
| 20 |
let instant = date.toTemporalInstant(); |
| 21 |
let zonedDateTime = instant.toZonedDateTimeISO(timeZone); |
| 22 |
let plainDateTime = zonedDateTime.toPlainDateTime(); |
| 23 |
let plainDate = zonedDateTime.toPlainDate(); |
| 24 |
let plainTime = zonedDateTime.toPlainTime(); |
| 25 |
|
| 26 |
for (let locale of locales) { |
| 27 |
for (let calendar of Intl.supportedValuesOf("calendar")) { |
| 28 |
// Calendar must match for YearMonth and MonthDay. |
| 29 |
// |
| 30 |
// https://github.com/js-temporal/proposal-temporal-v2/issues/29 |
| 31 |
let calendarDate = plainDate.withCalendar(calendar); |
| 32 |
let calendarYearMonth = calendarDate.toPlainYearMonth(); |
| 33 |
let calendarMonthDay = calendarDate.toPlainMonthDay(); |
| 34 |
|
| 35 |
assertEq( |
| 36 |
instant.toLocaleString(locale, {timeZone, calendar}), |
| 37 |
date.toLocaleString(locale, {timeZone, calendar}) |
| 38 |
); |
| 39 |
assertEq( |
| 40 |
zonedDateTime.toLocaleString(locale, {calendar}), |
| 41 |
date.toLocaleString(locale, {timeZone, calendar, timeZoneName: "short"}) |
| 42 |
); |
| 43 |
assertEq( |
| 44 |
plainDateTime.toLocaleString(locale, {timeZone, calendar}), |
| 45 |
date.toLocaleString(locale, {timeZone, calendar}) |
| 46 |
); |
| 47 |
assertEq( |
| 48 |
plainDate.toLocaleString(locale, {timeZone, calendar}), |
| 49 |
date.toLocaleDateString(locale, {timeZone, calendar}) |
| 50 |
); |
| 51 |
assertEq( |
| 52 |
plainTime.toLocaleString(locale, {timeZone, calendar}), |
| 53 |
date.toLocaleTimeString(locale, {timeZone, calendar}) |
| 54 |
); |
| 55 |
assertEq( |
| 56 |
calendarYearMonth.toLocaleString(locale, {timeZone, calendar}), |
| 57 |
date.toLocaleDateString(locale, {timeZone, calendar, year: "numeric", month: "numeric"}) |
| 58 |
); |
| 59 |
|
| 60 |
// ICU4X and ICU4C don't agree on calendar computations for islamic-umalqura. |
| 61 |
// |
| 62 |
// See <https://github.com/unicode-org/icu4x/issues/4982>. |
| 63 |
// |
| 64 |
// ICU4X and ICU4C are possibly both wrong for dates around 1970 when |
| 65 |
// comparing to these comparison charts: |
| 66 |
// https://web.archive.org/web/20150324181645fw_/http://www.staff.science.uu.nl/~gent0113/islam/downloads/ksa_calendar_1356_1411.pdf |
| 67 |
// |
| 68 |
// Also see: |
| 69 |
// https://web.archive.org/web/20110611040922if_/http://www.staff.science.uu.nl:80/~gent0113/islam/ummalqura.htm |
| 70 |
if (calendar !== "islamic-umalqura") { |
| 71 |
assertEq( |
| 72 |
calendarMonthDay.toLocaleString(locale, {timeZone, calendar}), |
| 73 |
date.toLocaleDateString(locale, {timeZone, calendar, month: "numeric", day: "numeric"}), |
| 74 |
); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (typeof reportCompare === "function") |
| 80 |
reportCompare(true, true); |