month-day.js (3193B)
1 // |reftest| skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl")) 2 3 // Test formatting for Temporal.MonthDay. 4 5 const locales = [ 6 "en", 7 "de", 8 "fr", 9 "es", 10 "ar", 11 "th", 12 "zh", 13 "ja", 14 ]; 15 16 const options = [ 17 { 18 month: "long", 19 }, 20 { 21 month: "numeric", 22 day: "numeric", 23 }, 24 ]; 25 26 const dateStyles = [ 27 "full", "long", "medium", "short" 28 ]; 29 30 // The current implementation computes the skeleton from the resolved pattern, 31 // which doesn't always produce the correct results. It'd be better to compute 32 // the skeleton from ICU's "DateTimeSkeletons" resource table. This requires 33 // writing manual resource lookup code, though. And "DateTimeSkeletons" doesn't 34 // (yet) contain the correct skeletons in all cases. For more info see these 35 // ICU and CLDR bugs: 36 // - https://unicode-org.atlassian.net/browse/ICU-22867 37 // - https://unicode-org.atlassian.net/browse/CLDR-14993 38 // - https://unicode-org.atlassian.net/browse/CLDR-18136 39 40 const dateStyleToOptions = { 41 full: { 42 month: "long", 43 day: "numeric", 44 }, 45 long: { 46 month: "long", 47 day: "numeric", 48 }, 49 medium: { 50 month: "short", 51 day: "numeric", 52 }, 53 short: { 54 month: "numeric", 55 day: "numeric", 56 }, 57 overrides: { 58 de: { 59 medium: { 60 month: "2-digit", 61 day: "2-digit", 62 }, 63 short: { 64 month: "2-digit", 65 day: "2-digit", 66 }, 67 }, 68 ar: { 69 medium: { 70 month: "2-digit", 71 day: "2-digit", 72 }, 73 }, 74 zh: { 75 full: { 76 month: "numeric", 77 day: "numeric", 78 }, 79 long: { 80 month: "numeric", 81 day: "numeric", 82 }, 83 medium: { 84 month: "numeric", 85 day: "numeric", 86 }, 87 }, 88 ja: { 89 full: { 90 month: "numeric", 91 day: "numeric", 92 }, 93 long: { 94 month: "numeric", 95 day: "numeric", 96 }, 97 medium: { 98 month: "2-digit", 99 day: "2-digit", 100 }, 101 short: { 102 month: "2-digit", 103 day: "2-digit", 104 }, 105 }, 106 }, 107 }; 108 109 const timeZone = "UTC"; 110 111 let date = new Date(0); 112 let instant = date.toTemporalInstant(); 113 let zonedDateTime = instant.toZonedDateTimeISO(timeZone); 114 let plainDate = zonedDateTime.toPlainDate(); 115 116 for (let locale of locales) { 117 // Get the default calendar for |locale|. 118 let calendar = new Intl.DateTimeFormat(locale).resolvedOptions().calendar; 119 120 // Calendar must match for MonthDay. 121 // 122 // https://github.com/js-temporal/proposal-temporal-v2/issues/29 123 let plainMonthDay = plainDate.withCalendar(calendar).toPlainMonthDay(); 124 125 for (let opts of options) { 126 let expected = date.toLocaleDateString(locale, {timeZone, ...opts}); 127 assertEq(plainMonthDay.toLocaleString(locale, {timeZone, ...opts}), expected); 128 } 129 130 for (let dateStyle of dateStyles) { 131 let opts = dateStyleToOptions.overrides[locale]?.[dateStyle] ?? dateStyleToOptions[dateStyle]; 132 assertEq( 133 plainMonthDay.toLocaleString(locale, {timeZone, dateStyle}), 134 date.toLocaleDateString(locale, {timeZone, ...opts}), 135 `l=${locale}, s=${dateStyle}, o=${JSON.stringify(opts)}` 136 ); 137 } 138 } 139 140 if (typeof reportCompare === "function") 141 reportCompare(true, true);