summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DateTimeFormat/hourCycle.js
blob: 21414c72cf1f987493561ea2759641b28a560f3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// |reftest| skip-if(!this.hasOwnProperty("Intl"))

const hourCycleToH12Map = {
  "h11": true,
  "h12": true,
  "h23": false,
  "h24": false,
};

for (const key of Object.keys(hourCycleToH12Map)) {
  const langTag = "en-US";
  const loc = `${langTag}-u-hc-${key}`;

  const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"});
  const dtf2 = new Intl.DateTimeFormat(langTag, {hour: "numeric", hourCycle: key});
  assertEq(dtf.resolvedOptions().hourCycle, dtf2.resolvedOptions().hourCycle);
}


/* Legacy hour12 compatibility */

// When constructed with hourCycle option, resolvedOptions' hour12 is correct.
for (const key of Object.keys(hourCycleToH12Map)) {
  const dtf = new Intl.DateTimeFormat("en-US", {hour: "numeric", hourCycle: key});
  assertEq(dtf.resolvedOptions().hour12, hourCycleToH12Map[key]);
}

// When constructed with hour12 option, resolvedOptions' hourCycle is correct
for (const [key, value] of Object.entries(hourCycleToH12Map)) {
  const dtf = new Intl.DateTimeFormat("en-US", {hour: "numeric", hour12: value});
  assertEq(hourCycleToH12Map[dtf.resolvedOptions().hourCycle], value);
}

// When constructed with both hour12 and hourCycle options that don't match
// hour12 takes a precedence.
for (const [key, value] of Object.entries(hourCycleToH12Map)) {
  const dtf = new Intl.DateTimeFormat("en-US", {
    hour: "numeric",
    hourCycle: key,
    hour12: !value
  });
  assertEq(hourCycleToH12Map[dtf.resolvedOptions().hourCycle], !value);
  assertEq(dtf.resolvedOptions().hour12, !value);
}

// When constructed with hourCycle as extkey, resolvedOptions' hour12 is correct.
for (const [key, value] of Object.entries(hourCycleToH12Map)) {
  const langTag = "en-US";
  const loc = `${langTag}-u-hc-${key}`;

  const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"});
  assertEq(dtf.resolvedOptions().hour12, value);
}

const expectedValuesENUS = {
  h11: "0 AM",
  h12: "12 AM",
  h23: "00",
  h24: "24"
};

const exampleDate = new Date(2017, 10-1, 10, 0);
for (const [key, val] of Object.entries(expectedValuesENUS)) {
  assertEq(
    Intl.DateTimeFormat("en-US", {hour: "numeric", hourCycle: key}).format(exampleDate),
    val
  );
}

const invalidHourCycleValues = [
  "h28",
  "f28",
];

for (const key of invalidHourCycleValues) {
  const langTag = "en-US";
  const loc = `${langTag}-u-hc-${key}`;

  const dtf = new Intl.DateTimeFormat(loc, {hour: "numeric"});
  assertEq(dtf.resolvedOptions().hour12, true); // default value for en-US
  assertEq(dtf.resolvedOptions().hourCycle, "h12"); //default value for en-US
}

{
  // hourCycle is not present in resolvedOptions when the formatter has no hour field
  const options = Intl.DateTimeFormat("en-US", {hourCycle:"h11"}).resolvedOptions();
  assertEq("hourCycle" in options, false);
  assertEq("hour12" in options, false);
}

{
  // Make sure that hourCycle option overrides the unicode extension
  let dtf = Intl.DateTimeFormat("en-US-u-hc-h23", {hourCycle: "h24", hour: "numeric"});
  assertEq(
    dtf.resolvedOptions().hourCycle,
    "h24"
  );
}

{
  // Make sure that hour12 option overrides the unicode extension
  let dtf = Intl.DateTimeFormat("en-US-u-hc-h23", {hour12: true, hour: "numeric"});
  assertEq(
    dtf.resolvedOptions().hourCycle,
    "h12"
  );
}

{
  // Make sure that hour12 option overrides hourCycle options
  let dtf = Intl.DateTimeFormat("en-US",
    {hourCycle: "h12", hour12: false, hour: "numeric"});
  assertEq(
    dtf.resolvedOptions().hourCycle,
    "h23"
  );
}

{
  // Make sure that hour12 option overrides hourCycle options
  let dtf = Intl.DateTimeFormat("en-u-hc-h11", {hour: "numeric"});
  assertEq(
    dtf.resolvedOptions().locale,
    "en-u-hc-h11"
  );
}

{
  // Make sure that hour12 option overrides unicode extension
  let dtf = Intl.DateTimeFormat("en-u-hc-h11", {hour: "numeric", hourCycle: "h24"});
  assertEq(
    dtf.resolvedOptions().locale,
    "en"
  );
  assertEq(
    dtf.resolvedOptions().hourCycle,
    "h24"
  );
}

if (typeof reportCompare === "function")
    reportCompare(0, 0, "ok");