-
Notifications
You must be signed in to change notification settings - Fork 54
/
time-zone-pst.js
120 lines (99 loc) · 5.6 KB
/
time-zone-pst.js
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
// |reftest| skip-if(!xulRuntime.shell)
assertEq(/^(PST|PDT)$/.test(getTimeZone()), true,
"The default time zone is set to PST8PDT for all jstests (when run in the shell)");
const msPerMinute = 60 * 1000;
function shortTimeZone(str) {
return str.replace("(Pacific Standard Time)", "(PST)")
.replace("(Pacific Daylight Time)", "(PDT)");
}
function assertEqDate(dt, date, time) {
assertEq(shortTimeZone(dt.toString()), `${date} ${time}`);
assertEq(dt.toDateString(), date);
assertEq(shortTimeZone(dt.toTimeString()), time);
}
// PDT -> PST, using milliseconds from epoch.
{
let midnight = new Date(2016, Month.November, 6, 0, 0, 0, 0);
let midnightUTC = Date.UTC(2016, Month.November, 6, 0, 0, 0, 0);
// Ensure midnight time is correct.
assertEq(midnightUTC - midnight.getTime(), -7 * msPerHour);
let tests = [
{ offset: 0 * 60, date: "Sun Nov 06 2016", time: "00:00:00 GMT-0700 (PDT)" },
{ offset: 0 * 60 + 30, date: "Sun Nov 06 2016", time: "00:30:00 GMT-0700 (PDT)" },
{ offset: 1 * 60, date: "Sun Nov 06 2016", time: "01:00:00 GMT-0700 (PDT)" },
{ offset: 1 * 60 + 30, date: "Sun Nov 06 2016", time: "01:30:00 GMT-0700 (PDT)" },
{ offset: 2 * 60, date: "Sun Nov 06 2016", time: "01:00:00 GMT-0800 (PST)" },
{ offset: 2 * 60 + 30, date: "Sun Nov 06 2016", time: "01:30:00 GMT-0800 (PST)" },
{ offset: 3 * 60, date: "Sun Nov 06 2016", time: "02:00:00 GMT-0800 (PST)" },
{ offset: 3 * 60 + 30, date: "Sun Nov 06 2016", time: "02:30:00 GMT-0800 (PST)" },
{ offset: 4 * 60, date: "Sun Nov 06 2016", time: "03:00:00 GMT-0800 (PST)" },
{ offset: 4 * 60 + 30, date: "Sun Nov 06 2016", time: "03:30:00 GMT-0800 (PST)" },
];
for (let {offset, date, time} of tests) {
let dt = new Date(midnight.getTime() + offset * msPerMinute);
assertEqDate(dt, date, time);
}
}
// PDT -> PST, using local date-time.
{
let tests = [
{ offset: 0 * 60, date: "Sun Nov 06 2016", time: "00:00:00 GMT-0700 (PDT)" },
{ offset: 0 * 60 + 30, date: "Sun Nov 06 2016", time: "00:30:00 GMT-0700 (PDT)" },
{ offset: 1 * 60, date: "Sun Nov 06 2016", time: "01:00:00 GMT-0700 (PDT)" },
{ offset: 1 * 60 + 30, date: "Sun Nov 06 2016", time: "01:30:00 GMT-0700 (PDT)" },
{ offset: 2 * 60, date: "Sun Nov 06 2016", time: "02:00:00 GMT-0800 (PST)" },
{ offset: 2 * 60 + 30, date: "Sun Nov 06 2016", time: "02:30:00 GMT-0800 (PST)" },
{ offset: 3 * 60, date: "Sun Nov 06 2016", time: "03:00:00 GMT-0800 (PST)" },
{ offset: 3 * 60 + 30, date: "Sun Nov 06 2016", time: "03:30:00 GMT-0800 (PST)" },
{ offset: 4 * 60, date: "Sun Nov 06 2016", time: "04:00:00 GMT-0800 (PST)" },
{ offset: 4 * 60 + 30, date: "Sun Nov 06 2016", time: "04:30:00 GMT-0800 (PST)" },
];
for (let {offset, date, time} of tests) {
let dt = new Date(2016, Month.November, 6, (offset / 60)|0, (offset % 60), 0, 0);
assertEqDate(dt, date, time);
}
}
// PST -> PDT, using milliseconds from epoch.
{
let midnight = new Date(2016, Month.March, 13, 0, 0, 0, 0);
let midnightUTC = Date.UTC(2016, Month.March, 13, 0, 0, 0, 0);
// Ensure midnight time is correct.
assertEq(midnightUTC - midnight.getTime(), -8 * msPerHour);
let tests = [
{ offset: 0 * 60, date: "Sun Mar 13 2016", time: "00:00:00 GMT-0800 (PST)" },
{ offset: 0 * 60 + 30, date: "Sun Mar 13 2016", time: "00:30:00 GMT-0800 (PST)" },
{ offset: 1 * 60, date: "Sun Mar 13 2016", time: "01:00:00 GMT-0800 (PST)" },
{ offset: 1 * 60 + 30, date: "Sun Mar 13 2016", time: "01:30:00 GMT-0800 (PST)" },
{ offset: 2 * 60, date: "Sun Mar 13 2016", time: "03:00:00 GMT-0700 (PDT)" },
{ offset: 2 * 60 + 30, date: "Sun Mar 13 2016", time: "03:30:00 GMT-0700 (PDT)" },
{ offset: 3 * 60, date: "Sun Mar 13 2016", time: "04:00:00 GMT-0700 (PDT)" },
{ offset: 3 * 60 + 30, date: "Sun Mar 13 2016", time: "04:30:00 GMT-0700 (PDT)" },
{ offset: 4 * 60, date: "Sun Mar 13 2016", time: "05:00:00 GMT-0700 (PDT)" },
{ offset: 4 * 60 + 30, date: "Sun Mar 13 2016", time: "05:30:00 GMT-0700 (PDT)" },
];
for (let {offset, date, time} of tests) {
let dt = new Date(midnight.getTime() + offset * msPerMinute);
assertEqDate(dt, date, time);
}
}
// PST -> PDT, using local date-time.
{
let tests = [
{ offset: 0 * 60, date: "Sun Mar 13 2016", time: "00:00:00 GMT-0800 (PST)" },
{ offset: 0 * 60 + 30, date: "Sun Mar 13 2016", time: "00:30:00 GMT-0800 (PST)" },
{ offset: 1 * 60, date: "Sun Mar 13 2016", time: "01:00:00 GMT-0800 (PST)" },
{ offset: 1 * 60 + 30, date: "Sun Mar 13 2016", time: "01:30:00 GMT-0800 (PST)" },
{ offset: 2 * 60, date: "Sun Mar 13 2016", time: "03:00:00 GMT-0700 (PDT)" },
{ offset: 2 * 60 + 30, date: "Sun Mar 13 2016", time: "03:30:00 GMT-0700 (PDT)" },
{ offset: 3 * 60, date: "Sun Mar 13 2016", time: "03:00:00 GMT-0700 (PDT)" },
{ offset: 3 * 60 + 30, date: "Sun Mar 13 2016", time: "03:30:00 GMT-0700 (PDT)" },
{ offset: 4 * 60, date: "Sun Mar 13 2016", time: "04:00:00 GMT-0700 (PDT)" },
{ offset: 4 * 60 + 30, date: "Sun Mar 13 2016", time: "04:30:00 GMT-0700 (PDT)" },
];
for (let {offset, date, time} of tests) {
let dt = new Date(2016, Month.March, 13, (offset / 60)|0, (offset % 60), 0, 0);
assertEqDate(dt, date, time);
}
}
if (typeof reportCompare === "function")
reportCompare(true, true);