| 1 |
// |reftest| skip-if(!this.hasOwnProperty("Intl")) |
| 2 |
|
| 3 |
const hourCycleToH12Map = { |
| 4 |
"h11": true, |
| 5 |
"h12": true, |
| 6 |
"h23": false, |
| 7 |
"h24": false, |
| 8 |
}; |
| 9 |
|
| 10 |
for (const key of Object.keys(hourCycleToH12Map)) { |
| 11 |
const langTag = "en-US"; |
| 12 |
const loc = `${langTag}-u-hc-${key}`; |
| 13 |
|
| 14 |
const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"}); |
| 15 |
const dtf2 = new Intl.DateTimeFormat(langTag, {hour: "numeric", hourCycle: key}); |
| 16 |
assertEq(dtf.resolvedOptions().hourCycle, dtf2.resolvedOptions().hourCycle); |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
/* Legacy hour12 compatibility */ |
| 21 |
|
| 22 |
// When constructed with hourCycle option, resolvedOptions' hour12 is correct. |
| 23 |
for (const key of Object.keys(hourCycleToH12Map)) { |
| 24 |
const dtf = new Intl.DateTimeFormat("en-US", {hour: "numeric", hourCycle: key}); |
| 25 |
assertEq(dtf.resolvedOptions().hour12, hourCycleToH12Map[key]); |
| 26 |
} |
| 27 |
|
| 28 |
// When constructed with hour12 option, resolvedOptions' hourCycle is correct |
| 29 |
for (const [key, value] of Object.entries(hourCycleToH12Map)) { |
| 30 |
const dtf = new Intl.DateTimeFormat("en-US", {hour: "numeric", hour12: value}); |
| 31 |
assertEq(hourCycleToH12Map[dtf.resolvedOptions().hourCycle], value); |
| 32 |
} |
| 33 |
|
| 34 |
// When constructed with both hour12 and hourCycle options that don't match |
| 35 |
// hour12 takes a precedence. |
| 36 |
for (const [key, value] of Object.entries(hourCycleToH12Map)) { |
| 37 |
const dtf = new Intl.DateTimeFormat("en-US", { |
| 38 |
hour: "numeric", |
| 39 |
hourCycle: key, |
| 40 |
hour12: !value |
| 41 |
}); |
| 42 |
assertEq(hourCycleToH12Map[dtf.resolvedOptions().hourCycle], !value); |
| 43 |
assertEq(dtf.resolvedOptions().hour12, !value); |
| 44 |
} |
| 45 |
|
| 46 |
// When constructed with hourCycle as extkey, resolvedOptions' hour12 is correct. |
| 47 |
for (const [key, value] of Object.entries(hourCycleToH12Map)) { |
| 48 |
const langTag = "en-US"; |
| 49 |
const loc = `${langTag}-u-hc-${key}`; |
| 50 |
|
| 51 |
const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"}); |
| 52 |
assertEq(dtf.resolvedOptions().hour12, value); |
| 53 |
} |
| 54 |
|
| 55 |
const expectedValuesENUS = { |
| 56 |
h11: "0 AM", |
| 57 |
h12: "12 AM", |
| 58 |
h23: "00", |
| 59 |
h24: "24" |
| 60 |
}; |
| 61 |
|
| 62 |
const exampleDate = new Date(2017, 10-1, 10, 0); |
| 63 |
for (const [key, val] of Object.entries(expectedValuesENUS)) { |
| 64 |
assertEq( |
| 65 |
Intl.DateTimeFormat("en-US", {hour: "numeric", hourCycle: key}).format(exampleDate), |
| 66 |
val |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
const invalidHourCycleValues = [ |
| 71 |
"h28", |
| 72 |
"f28", |
| 73 |
]; |
| 74 |
|
| 75 |
for (const key of invalidHourCycleValues) { |
| 76 |
const langTag = "en-US"; |
| 77 |
const loc = `${langTag}-u-hc-${key}`; |
| 78 |
|
| 79 |
const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"}); |
| 80 |
assertEq(dtf.resolvedOptions().hour12, true); // default value for en-US |
| 81 |
assertEq(dtf.resolvedOptions().hourCycle, "h12"); //default value for en-US |
| 82 |
} |
| 83 |
|
| 84 |
{ |
| 85 |
// hourCycle is not present in resolvedOptions when the formatter has no hour field |
| 86 |
const options = Intl.DateTimeFormat("en-US", {hourCycle:"h11"}).resolvedOptions(); |
| 87 |
assertEq("hourCycle" in options, false); |
| 88 |
assertEq("hour12" in options, false); |
| 89 |
} |
| 90 |
|
| 91 |
{ |
| 92 |
// Make sure that hourCycle option overrides the unicode extension |
| 93 |
let dtf = Intl.DateTimeFormat("en-US-u-hc-h23", {hourCycle: "h24", hour: "numeric"}); |
| 94 |
assertEq( |
| 95 |
dtf.resolvedOptions().hourCycle, |
| 96 |
"h24" |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
{ |
| 101 |
// Make sure that hour12 option overrides the unicode extension |
| 102 |
let dtf = Intl.DateTimeFormat("en-US-u-hc-h23", {hour12: true, hour: "numeric"}); |
| 103 |
assertEq( |
| 104 |
dtf.resolvedOptions().hourCycle, |
| 105 |
"h12" |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
{ |
| 110 |
// Make sure that hour12 option overrides hourCycle options |
| 111 |
let dtf = Intl.DateTimeFormat("en-US", |
| 112 |
{hourCycle: "h12", hour12: false, hour: "numeric"}); |
| 113 |
assertEq( |
| 114 |
dtf.resolvedOptions().hourCycle, |
| 115 |
"h23" |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
{ |
| 120 |
// Make sure that hour12 option overrides hourCycle options |
| 121 |
let dtf = Intl.DateTimeFormat("en-u-hc-h11", {hour: "numeric"}); |
| 122 |
assertEq( |
| 123 |
dtf.resolvedOptions().locale, |
| 124 |
"en-u-hc-h11" |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
{ |
| 129 |
// Make sure that hour12 option overrides unicode extension |
| 130 |
let dtf = Intl.DateTimeFormat("en-u-hc-h11", {hour: "numeric", hourCycle: "h24"}); |
| 131 |
assertEq( |
| 132 |
dtf.resolvedOptions().locale, |
| 133 |
"en" |
| 134 |
); |
| 135 |
assertEq( |
| 136 |
dtf.resolvedOptions().hourCycle, |
| 137 |
"h24" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
if (typeof reportCompare === "function") |
| 142 |
reportCompare(0, 0, "ok"); |