day-period-hour-cycle.js (3677B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 3 const { 4 Year, Month, Day, DayPeriod, Hour, Minute, Literal 5 } = DateTimeFormatParts; 6 7 // If the locale defaults to a 24-hour-cycle, the "dayPeriod" option is ignored if an "hour" option 8 // is also present, unless the hour-cycle is manually set to a 12-hour-cycle. 9 10 const tests = [ 11 { 12 date: new Date("2019-01-01T12:00:00"), 13 options: { dayPeriod: "short", hour: "numeric", }, 14 locales: { 15 en: [Hour("12"), Literal(" "), DayPeriod("noon")], 16 de: [Hour("12"), Literal(" Uhr")], 17 }, 18 }, 19 { 20 date: new Date("2019-01-01T12:00:00"), 21 options: { dayPeriod: "short", hour: "numeric", hour12: true }, 22 locales: { 23 en: [Hour("12"), Literal(" "), DayPeriod("noon")], 24 de: [Hour("12"), Literal(" Uhr "), DayPeriod("mittags")], 25 }, 26 }, 27 { 28 date: new Date("2019-01-01T12:00:00"), 29 options: { dayPeriod: "short", hour: "numeric", hour12: false }, 30 locales: { 31 en: [Hour("12")], 32 de: [Hour("12"), Literal(" Uhr")], 33 }, 34 }, 35 { 36 date: new Date("2019-01-01T12:00:00"), 37 options: { dayPeriod: "short", hour: "numeric", hourCycle: "h12" }, 38 locales: { 39 en: [Hour("12"), Literal(" "), DayPeriod("noon")], 40 de: [Hour("12"), Literal(" Uhr "), DayPeriod("mittags")], 41 }, 42 }, 43 { 44 date: new Date("2019-01-01T12:00:00"), 45 options: { dayPeriod: "short", hour: "numeric", hourCycle: "h11" }, 46 locales: { 47 en: [Hour("0"), Literal(" "), DayPeriod("noon")], 48 de: [Hour("0"), Literal(" Uhr "), DayPeriod("mittags")], 49 }, 50 }, 51 { 52 date: new Date("2019-01-01T12:00:00"), 53 options: { dayPeriod: "short", hour: "numeric", hourCycle: "h23" }, 54 locales: { 55 en: [Hour("12")], 56 de: [Hour("12"), Literal(" Uhr")], 57 }, 58 }, 59 { 60 date: new Date("2019-01-01T12:00:00"), 61 options: { dayPeriod: "short", hour: "numeric", hourCycle: "h24" }, 62 locales: { 63 en: [Hour("12")], 64 de: [Hour("12"), Literal(" Uhr")], 65 }, 66 }, 67 68 // The default hour-cycle is irrelevant when an "hour" option isn't present. 69 { 70 date: new Date("2019-01-01T12:00:00"), 71 options: { dayPeriod: "short", day: "numeric", month: "numeric", year: "numeric" }, 72 locales: { 73 en: [Month("1"), Literal("/"), Day("1"), Literal("/"), Year("2019"), Literal(", "), DayPeriod("noon")], 74 de: [Day("1"), Literal("."), Month("1"), Literal("."), Year("2019"), Literal(", "), DayPeriod("mittags")], 75 }, 76 }, 77 78 // ICU replacement pattern for missing <appendItem> entries in CLDR. 79 { 80 date: new Date("2019-01-01T12:00:00"), 81 options: { dayPeriod: "short", minute: "numeric" }, 82 locales: { 83 en: [Minute("0"), Literal(" (AM/PM: "), DayPeriod("noon"), Literal(")")], 84 de: [Minute("0"), Literal(" (Tageshälfte: "), DayPeriod("mittags"), Literal(")")], 85 }, 86 }, 87 ]; 88 89 for (let {date, options, locales} of tests) { 90 for (let [locale, parts] of Object.entries(locales)) { 91 let dtf = new Intl.DateTimeFormat(locale, options); 92 93 assertEq(dtf.format(date), parts.map(({value}) => value).join(""), 94 `locale=${locale}, date=${date}, options=${JSON.stringify(options)}`); 95 96 assertDeepEq(dtf.formatToParts(date), parts, 97 `locale=${locale}, date=${date}, options=${JSON.stringify(options)}`); 98 } 99 } 100 101 if (typeof reportCompare === "function") 102 reportCompare(0, 0, "ok");