| 1 |
// |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl")) |
| 2 |
|
| 3 |
// Ensure Intl.DateTimeFormat and Temporal return consistent year-month-day values |
| 4 |
// for reasonable dates. |
| 5 |
|
| 6 |
const weekdays = [ |
| 7 |
"Monday", |
| 8 |
"Tuesday", |
| 9 |
"Wednesday", |
| 10 |
"Thursday", |
| 11 |
"Friday", |
| 12 |
"Saturday", |
| 13 |
"Sunday", |
| 14 |
]; |
| 15 |
|
| 16 |
// Map Hebrew months to their corresponding month code. |
| 17 |
const hebrewMonths = { |
| 18 |
"Tishri": "M01", |
| 19 |
"Heshvan": "M02", |
| 20 |
"Kislev": "M03", |
| 21 |
"Tevet": "M04", |
| 22 |
"Shevat": "M05", |
| 23 |
"Adar I": "M05L", |
| 24 |
"Adar II": "M06", |
| 25 |
"Adar": "M06", |
| 26 |
"Nisan": "M07", |
| 27 |
"Iyar": "M08", |
| 28 |
"Sivan": "M09", |
| 29 |
"Tamuz": "M10", |
| 30 |
"Av": "M11", |
| 31 |
"Elul": "M12", |
| 32 |
}; |
| 33 |
|
| 34 |
// Extract date information from a to parts formatted date. |
| 35 |
function dateFromParts(parts) { |
| 36 |
let relatedYear = undefined; |
| 37 |
let year = undefined; |
| 38 |
let monthCode = ""; |
| 39 |
let day = 0; |
| 40 |
let dayOfWeek = -1; |
| 41 |
|
| 42 |
for (let {type, value} of parts) { |
| 43 |
switch (type) { |
| 44 |
case "weekday": |
| 45 |
dayOfWeek = weekdays.indexOf(value); |
| 46 |
break; |
| 47 |
case "year": |
| 48 |
year = Number(value); |
| 49 |
break; |
| 50 |
case "relatedYear": |
| 51 |
relatedYear = Number(value); |
| 52 |
break; |
| 53 |
case "month": { |
| 54 |
if (value in hebrewMonths) { |
| 55 |
monthCode = hebrewMonths[value]; |
| 56 |
} else { |
| 57 |
// Chinese/Dangi leap months end with "bis", from Latin "bis" = "twice". |
| 58 |
let leapMonth = value.endsWith("bis"); |
| 59 |
if (leapMonth) { |
| 60 |
value = value.slice(0, -"bis".length); |
| 61 |
} |
| 62 |
monthCode = "M" + value.padStart(2, "0") + (leapMonth ? "L" : ""); |
| 63 |
} |
| 64 |
break; |
| 65 |
} |
| 66 |
case "day": |
| 67 |
day = Number(value); |
| 68 |
break; |
| 69 |
case "era": |
| 70 |
case "literal": |
| 71 |
continue; |
| 72 |
default: throw new Error("bad part: " + type); |
| 73 |
} |
| 74 |
} |
| 75 |
assertEq(dayOfWeek >= 0, true); |
| 76 |
assertEq(monthCode.length > 0, true, JSON.stringify(parts)); |
| 77 |
assertEq(day > 0, true); |
| 78 |
|
| 79 |
dayOfWeek += 1; |
| 80 |
|
| 81 |
return { |
| 82 |
relatedYear, |
| 83 |
year, |
| 84 |
monthCode, |
| 85 |
day, |
| 86 |
dayOfWeek, |
| 87 |
}; |
| 88 |
} |
| 89 |
|
| 90 |
const tests = { |
| 91 |
buddhist: [ |
| 92 |
// Date ranges in 1500..2500 where ICU4C and ICU4X compute different results. |
| 93 |
// |
| 94 |
// NOTE: These are dates before the Gregorian change date October 15, 1582. |
| 95 |
{ |
| 96 |
start: {iso: "1500-01-01", year: 2043, monthCode: "M01", day: 1}, |
| 97 |
end: {iso: "1582-10-14", year: 2125, monthCode: "M10", day: 14}, |
| 98 |
}, |
| 99 |
], |
| 100 |
chinese: [ |
| 101 |
// Date ranges in 1900..2100 where ICU4C and ICU4X compute different results. |
| 102 |
{ |
| 103 |
start: {iso: "1906-04-23", relatedYear: 1906, monthCode: "M04", day: 1}, |
| 104 |
end: {iso: "1906-05-22", relatedYear: 1906, monthCode: "M04", day: 30}, |
| 105 |
}, |
| 106 |
{ |
| 107 |
start: {iso: "1917-03-23", relatedYear: 1917, monthCode: "M02L", day: 1}, |
| 108 |
end: {iso: "1917-05-20", relatedYear: 1917, monthCode: "M03", day: 30}, |
| 109 |
}, |
| 110 |
{ |
| 111 |
start: {iso: "1922-06-25", relatedYear: 1922, monthCode: "M05L", day: 1}, |
| 112 |
end: {iso: "1922-08-22", relatedYear: 1922, monthCode: "M06", day: 30}, |
| 113 |
}, |
| 114 |
{ |
| 115 |
start: {iso: "1954-02-03", relatedYear: 1954, monthCode: "M01", day: 1}, |
| 116 |
end: {iso: "1954-03-04", relatedYear: 1954, monthCode: "M01", day: 30}, |
| 117 |
}, |
| 118 |
{ |
| 119 |
start: {iso: "1955-02-22", relatedYear: 1955, monthCode: "M02", day: 1}, |
| 120 |
end: {iso: "1955-03-23", relatedYear: 1955, monthCode: "M02", day: 30}, |
| 121 |
}, |
| 122 |
{ |
| 123 |
start: {iso: "1987-07-26", relatedYear: 1987, monthCode: "M06L", day: 1}, |
| 124 |
end: {iso: "1987-09-22", relatedYear: 1987, monthCode: "M07", day: 30}, |
| 125 |
}, |
| 126 |
{ |
| 127 |
start: {iso: "1999-01-17", relatedYear: 1998, monthCode: "M12", day: 1}, |
| 128 |
end: {iso: "1999-02-15", relatedYear: 1998, monthCode: "M12", day: 30}, |
| 129 |
}, |
| 130 |
{ |
| 131 |
start: {iso: "2012-08-17", relatedYear: 2012, monthCode: "M07", day: 1}, |
| 132 |
end: {iso: "2012-09-15", relatedYear: 2012, monthCode: "M07", day: 30}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
start: {iso: "2018-11-07", relatedYear: 2018, monthCode: "M09", day: 30}, |
| 136 |
end: {iso: "2018-12-06", relatedYear: 2018, monthCode: "M10", day: 29}, |
| 137 |
}, |
| 138 |
{ |
| 139 |
start: {iso: "2027-02-06", relatedYear: 2027, monthCode: "M01", day: 1}, |
| 140 |
end: {iso: "2027-03-07", relatedYear: 2027, monthCode: "M01", day: 30}, |
| 141 |
}, |
| 142 |
{ |
| 143 |
start: {iso: "2030-02-02", relatedYear: 2029, monthCode: "M12", day: 30}, |
| 144 |
end: {iso: "2030-03-03", relatedYear: 2030, monthCode: "M01", day: 29}, |
| 145 |
}, |
| 146 |
{ |
| 147 |
start: {iso: "2057-09-28", relatedYear: 2057, monthCode: "M09", day: 1}, |
| 148 |
end: {iso: "2057-10-27", relatedYear: 2057, monthCode: "M09", day: 30}, |
| 149 |
}, |
| 150 |
{ |
| 151 |
start: {iso: "2070-03-12", relatedYear: 2070, monthCode: "M02", day: 1}, |
| 152 |
end: {iso: "2070-04-10", relatedYear: 2070, monthCode: "M02", day: 30}, |
| 153 |
}, |
| 154 |
], |
| 155 |
dangi: [ |
| 156 |
// Date ranges in 1900..2100 where ICU4C and ICU4X compute different results. |
| 157 |
{ |
| 158 |
start: {iso: "1904-01-17", relatedYear: 1903, monthCode: "M11", day: 30}, |
| 159 |
end: {iso: "1904-02-15", relatedYear: 1903, monthCode: "M12", day: 29}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
start: {iso: "1904-11-07", relatedYear: 1904, monthCode: "M09", day: 30}, |
| 163 |
end: {iso: "1904-12-06", relatedYear: 1904, monthCode: "M10", day: 29}, |
| 164 |
}, |
| 165 |
{ |
| 166 |
start: {iso: "1905-05-04", relatedYear: 1905, monthCode: "M03", day: 30}, |
| 167 |
end: {iso: "1905-06-02", relatedYear: 1905, monthCode: "M04", day: 29}, |
| 168 |
}, |
| 169 |
{ |
| 170 |
start: {iso: "1908-04-30", relatedYear: 1908, monthCode: "M03", day: 30}, |
| 171 |
end: {iso: "1908-05-29", relatedYear: 1908, monthCode: "M04", day: 29}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
start: {iso: "1911-12-20", relatedYear: 1911, monthCode: "M10", day: 30}, |
| 175 |
end: {iso: "1912-01-18", relatedYear: 1911, monthCode: "M11", day: 29}, |
| 176 |
}, |
| 177 |
{ |
| 178 |
start: {iso: "2017-02-26", relatedYear: 2017, monthCode: "M02", day: 1}, |
| 179 |
end: {iso: "2017-03-27", relatedYear: 2017, monthCode: "M02", day: 30}, |
| 180 |
}, |
| 181 |
{ |
| 182 |
start: {iso: "2051-08-06", relatedYear: 2051, monthCode: "M06", day: 30}, |
| 183 |
end: {iso: "2051-09-04", relatedYear: 2051, monthCode: "M07", day: 29}, |
| 184 |
}, |
| 185 |
{ |
| 186 |
start: {iso: "2051-11-03", relatedYear: 2051, monthCode: "M10", day: 1}, |
| 187 |
end: {iso: "2051-12-02", relatedYear: 2051, monthCode: "M10", day: 30}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
start: {iso: "2097-01-13", relatedYear: 2096, monthCode: "M12", day: 1}, |
| 191 |
end: {iso: "2097-02-11", relatedYear: 2096, monthCode: "M12", day: 30}, |
| 192 |
}, |
| 193 |
], |
| 194 |
hebrew: [ |
| 195 |
// Date ranges in 1500..2500 where ICU4C and ICU4X compute different results. |
| 196 |
// ICU bug report: <https://unicode-org.atlassian.net/browse/ICU-23069>. |
| 197 |
{ |
| 198 |
start: {iso: "1700-11-12", year: 5461, monthCode: "M03", day: 1}, |
| 199 |
end: {iso: "1701-12-01", year: 5462, monthCode: "M02", day: 30}, |
| 200 |
}, |
| 201 |
{ |
| 202 |
start: {iso: "1798-11-09", year: 5559, monthCode: "M03", day: 1}, |
| 203 |
end: {iso: "1799-11-28", year: 5560, monthCode: "M02", day: 30}, |
| 204 |
}, |
| 205 |
{ |
| 206 |
start: {iso: "2045-11-10", year: 5806, monthCode: "M03", day: 1}, |
| 207 |
end: {iso: "2046-11-29", year: 5807, monthCode: "M02", day: 30}, |
| 208 |
}, |
| 209 |
{ |
| 210 |
start: {iso: "2292-11-11", year: 6053, monthCode: "M03", day: 1}, |
| 211 |
end: {iso: "2293-11-30", year: 6054, monthCode: "M02", day: 30}, |
| 212 |
}, |
| 213 |
], |
| 214 |
"islamic": [ |
| 215 |
// TODO: Not yet supported. |
| 216 |
|
| 217 |
// Date ranges in 2000..2030 where ICU4C and ICU4X compute different results. |
| 218 |
// { |
| 219 |
// start: {iso: "2000-01-01", year: 1420, monthCode: "M09", day: 23}, |
| 220 |
// end: {iso: "2029-12-31", year: 1451, monthCode: "M08", day: 25}, |
| 221 |
// }, |
| 222 |
], |
| 223 |
"islamic-umalqura": [ |
| 224 |
// TODO: Not yet supported. |
| 225 |
|
| 226 |
// Date ranges in 2000..2030 where ICU4C and ICU4X compute different results. |
| 227 |
// { |
| 228 |
// start: {iso: "2000-02-06", year: 1420, monthCode: "M11", day: 1}, |
| 229 |
// end: {iso: "2000-03-06", year: 1420, monthCode: "M11", day: 30}, |
| 230 |
// }, |
| 231 |
// { |
| 232 |
// start: {iso: "2000-09-28", year: 1421, monthCode: "M06", day: 30}, |
| 233 |
// end: {iso: "2000-10-27", year: 1421, monthCode: "M07", day: 29}, |
| 234 |
// }, |
| 235 |
// { |
| 236 |
// start: {iso: "2001-10-17", year: 1422, monthCode: "M07", day: 30}, |
| 237 |
// end: {iso: "2001-11-15", year: 1422, monthCode: "M08", day: 29}, |
| 238 |
// }, |
| 239 |
// { |
| 240 |
// start: {iso: "2006-06-26", year: 1427, monthCode: "M06", day: 1}, |
| 241 |
// end: {iso: "2006-07-25", year: 1427, monthCode: "M06", day: 30}, |
| 242 |
// }, |
| 243 |
// { |
| 244 |
// start: {iso: "2024-12-02", year: 1446, monthCode: "M05", day: 30}, |
| 245 |
// end: {iso: "2024-12-31", year: 1446, monthCode: "M06", day: 29}, |
| 246 |
// }, |
| 247 |
// { |
| 248 |
// start: {iso: "2025-01-30", year: 1446, monthCode: "M08", day: 1}, |
| 249 |
// end: {iso: "2025-02-28", year: 1446, monthCode: "M08", day: 30}, |
| 250 |
// }, |
| 251 |
// { |
| 252 |
// start: {iso: "2029-08-11", year: 1451, monthCode: "M04", day: 1}, |
| 253 |
// end: {iso: "2029-09-09", year: 1451, monthCode: "M04", day: 30}, |
| 254 |
// }, |
| 255 |
// { |
| 256 |
// start: {iso: "2029-11-07", year: 1451, monthCode: "M07", day: 1}, |
| 257 |
// end: {iso: "2029-12-06", year: 1451, monthCode: "M07", day: 30}, |
| 258 |
// }, |
| 259 |
], |
| 260 |
japanese: [ |
| 261 |
// Date ranges in 1500..2500 where ICU4C and ICU4X compute different results. |
| 262 |
// |
| 263 |
// NOTE: These are dates before the Gregorian change date October 15, 1582. |
| 264 |
{ |
| 265 |
start: {iso: "1500-01-01", monthCode: "M01", day: 1}, |
| 266 |
end: {iso: "1582-10-14", monthCode: "M10", day: 14}, |
| 267 |
}, |
| 268 |
], |
| 269 |
persian: [ |
| 270 |
// Date ranges in 1500..2500 where ICU4C and ICU4X compute different results. |
| 271 |
// More info: https://github.com/unicode-org/icu4x/issues/4713 |
| 272 |
{ |
| 273 |
start: {iso: "2124-03-20", year: 1503, monthCode: "M01", day: 1}, |
| 274 |
end: {iso: "2125-03-20", year: 1503, monthCode: "M12", day: 30}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
start: {iso: "2223-03-21", year: 1602, monthCode: "M01", day: 1}, |
| 278 |
end: {iso: "2224-03-20", year: 1602, monthCode: "M12", day: 30}, |
| 279 |
}, |
| 280 |
{ |
| 281 |
start: {iso: "2256-03-20", year: 1635, monthCode: "M01", day: 1}, |
| 282 |
end: {iso: "2257-03-20", year: 1635, monthCode: "M12", day: 30}, |
| 283 |
}, |
| 284 |
{ |
| 285 |
start: {iso: "2289-03-20", year: 1668, monthCode: "M01", day: 1}, |
| 286 |
end: {iso: "2290-03-20", year: 1668, monthCode: "M12", day: 30}, |
| 287 |
}, |
| 288 |
{ |
| 289 |
start: {iso: "2322-03-21", year: 1701, monthCode: "M01", day: 1}, |
| 290 |
end: {iso: "2323-03-21", year: 1701, monthCode: "M12", day: 30}, |
| 291 |
}, |
| 292 |
{ |
| 293 |
start: {iso: "2355-03-21", year: 1734, monthCode: "M01", day: 1}, |
| 294 |
end: {iso: "2356-03-20", year: 1734, monthCode: "M12", day: 30}, |
| 295 |
}, |
| 296 |
{ |
| 297 |
start: {iso: "2388-03-20", year: 1767, monthCode: "M01", day: 1}, |
| 298 |
end: {iso: "2389-03-20", year: 1767, monthCode: "M12", day: 30}, |
| 299 |
}, |
| 300 |
{ |
| 301 |
start: {iso: "2421-03-20", year: 1800, monthCode: "M01", day: 1}, |
| 302 |
end: {iso: "2422-03-20", year: 1800, monthCode: "M12", day: 30}, |
| 303 |
}, |
| 304 |
{ |
| 305 |
start: {iso: "2454-03-20", year: 1833, monthCode: "M01", day: 1}, |
| 306 |
end: {iso: "2455-03-20", year: 1833, monthCode: "M12", day: 30}, |
| 307 |
}, |
| 308 |
{ |
| 309 |
start: {iso: "2487-03-20", year: 1866, monthCode: "M01", day: 1}, |
| 310 |
end: {iso: "2488-03-19", year: 1866, monthCode: "M12", day: 30}, |
| 311 |
}, |
| 312 |
], |
| 313 |
roc: [ |
| 314 |
// Date ranges in 1500..2500 where ICU4C and ICU4X compute different results. |
| 315 |
// |
| 316 |
// NOTE: These are dates before the Gregorian change date October 15, 1582. |
| 317 |
{ |
| 318 |
start: {iso: "1500-01-01", eraYear: 412, monthCode: "M01", day: 1}, |
| 319 |
end: {iso: "1582-10-14", eraYear: 330, monthCode: "M10", day: 14}, |
| 320 |
}, |
| 321 |
], |
| 322 |
}; |
| 323 |
|
| 324 |
for (let [calendar, dates] of Object.entries(tests)) { |
| 325 |
let dtf = new Intl.DateTimeFormat("en", { |
| 326 |
timeZone: "UTC", |
| 327 |
calendar, |
| 328 |
year: "numeric", |
| 329 |
month: "numeric", |
| 330 |
day: "numeric", |
| 331 |
weekday: "long", |
| 332 |
}); |
| 333 |
|
| 334 |
for (let {start, end} of dates) { |
| 335 |
// Compute from start date. |
| 336 |
let isoStartDate = Temporal.PlainDate.from(start.iso); |
| 337 |
let startDate = isoStartDate.withCalendar(calendar); |
| 338 |
let startDateParts = dtf.formatToParts(startDate); |
| 339 |
|
| 340 |
// Compute from end date. |
| 341 |
let isoEndDate = Temporal.PlainDate.from(end.iso); |
| 342 |
let endDate = isoEndDate.withCalendar(calendar); |
| 343 |
let endDateParts = dtf.formatToParts(endDate); |
| 344 |
|
| 345 |
// Compute from ranges. |
| 346 |
let rangeParts = dtf.formatRangeToParts(startDate, endDate); |
| 347 |
let startRangeDateParts = rangeParts.filter(({source}) => source !== "endRange"); |
| 348 |
let endRangeDateParts = rangeParts.filter(({source}) => source !== "startRange"); |
| 349 |
|
| 350 |
// Entries to check. |
| 351 |
let entries = [ |
| 352 |
{ |
| 353 |
date: startDate, |
| 354 |
parts: startDateParts, |
| 355 |
expected: start, |
| 356 |
}, |
| 357 |
{ |
| 358 |
date: endDate, |
| 359 |
parts: endDateParts, |
| 360 |
expected: end, |
| 361 |
}, |
| 362 |
{ |
| 363 |
date: startDate, |
| 364 |
parts: startRangeDateParts, |
| 365 |
expected: start, |
| 366 |
}, |
| 367 |
{ |
| 368 |
date: endDate, |
| 369 |
parts: endRangeDateParts, |
| 370 |
expected: end, |
| 371 |
}, |
| 372 |
]; |
| 373 |
|
| 374 |
for (let {date, parts, expected} of entries) { |
| 375 |
// Ensure Temporal matches |expected|. |
| 376 |
if (expected.year !== undefined) { |
| 377 |
assertEq(date.year, expected.year); |
| 378 |
} |
| 379 |
if (expected.eraYear !== undefined) { |
| 380 |
assertEq(date.eraYear, expected.eraYear); |
| 381 |
} |
| 382 |
if (expected.relatedYear !== undefined) { |
| 383 |
assertEq(date.with({month: 1}).withCalendar("iso8601").year, expected.relatedYear); |
| 384 |
} |
| 385 |
assertEq(date.monthCode, expected.monthCode); |
| 386 |
assertEq(date.day, expected.day); |
| 387 |
|
| 388 |
// Ensure Intl.DateTimeFormat matches |expected|. |
| 389 |
let partsDate = dateFromParts(parts); |
| 390 |
|
| 391 |
if (expected.year !== undefined) { |
| 392 |
assertEq(partsDate.year, expected.year); |
| 393 |
} |
| 394 |
if (expected.eraYear !== undefined) { |
| 395 |
assertEq(partsDate.year, expected.eraYear); |
| 396 |
} |
| 397 |
if (partsDate.relatedYear !== undefined) { |
| 398 |
// NB: relatedYear isn't used for range formats with chinese/dangi calendars. |
| 399 |
assertEq(partsDate.relatedYear, expected.relatedYear); |
| 400 |
} |
| 401 |
assertEq(partsDate.monthCode, expected.monthCode); |
| 402 |
assertEq(partsDate.day, expected.day); |
| 403 |
} |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
if (typeof reportCompare === "function") |
| 408 |
reportCompare(true, true); |