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
|
// |reftest| skip-if(!this.hasOwnProperty("Intl"))
const defaultLocale = "en-US";
const defaultDate = Date.UTC(2012, 12-1, 6, 12, 0, 0);
const defaultOptions = { timeZoneName: "short" };
const tests = [
{
timeZone: "UTC",
result: "12/6/2012, GMT",
},
{
timeZone: "America/Los_Angeles",
result: "12/6/2012, PST",
},
{
timeZone: "Europe/Berlin", locale: "de",
options: { timeZoneName: "short" },
result: "6.12.2012, MEZ",
},
{
timeZone: "Europe/Paris", locale: "fr",
options: { timeZoneName: "long" },
result: "06/12/2012 à heure normale d’Europe centrale",
},
{
timeZone: "Asia/Shanghai", locale: "zh-Hans-CN",
options: { timeZoneName: "long" },
result: "2012/12/6 中国标准时间",
},
{
timeZone: { toString: () => "Australia/Melbourne" }, locale: "en-AU",
result: "06/12/2012, AEDT",
},
];
for (let {timeZone, result, locale = defaultLocale, date = defaultDate, options = defaultOptions} of tests) {
let s = new Date(date).toLocaleDateString(locale, Object.assign({timeZone}, options));
assertEq(s, result);
}
// |undefined| or absent "timeZone" option selects the default time zone.
{
let locale = defaultLocale;
let date = defaultDate;
let options = defaultOptions;
let absentTz = new Date(date).toLocaleDateString(locale, Object.assign({}, options));
let undefinedTz = new Date(date).toLocaleDateString(locale, Object.assign({timeZone: undefined}, options));
assertEq(undefinedTz, absentTz);
}
// RangeError is thrown for invalid time zone names.
for (let timeZone of ["", "undefined", "UTC\0", "Vienna", "Africa", "America/NewYork"]) {
assertThrowsInstanceOf(() => {
new Date(defaultDate).toLocaleDateString(undefined, {timeZone});
}, RangeError);
}
// RangeError is thrown for these values, because ToString(<value>)
// isn't a valid time zone name.
for (let timeZone of [null, 0, 0.5, true, false, [], {}, function() {}]) {
assertThrowsInstanceOf(() => {
new Date(defaultDate).toLocaleDateString(undefined, {timeZone});
}, RangeError);
}
// ToString(<symbol>) throws TypeError.
assertThrowsInstanceOf(() => {
new Date(defaultDate).toLocaleDateString(undefined, {timeZone: Symbol()});
}, TypeError);
if (typeof reportCompare === "function")
reportCompare(0, 0, "ok");
|