-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
181 lines (169 loc) · 4 KB
/
index.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
'use strict';
const timeZones = [
'GMT-12:00, Dateline (Eniwetok)',
'GMT-11:00, Samoa (Samoa)',
'GMT-10:00, Hawaii (Honolulu)',
'GMT-09:00, Alaska (Ancorage)',
'GMT-08:00, Pacific (San Jose)',
'GMT-07:00, Mountain (Arizona)',
'GMT-07:00, Mountain (Denver)',
'GMT-06:00, Central (Chicago)',
'GMT-06:00, Mexico (Mexico City,Tegucigalpa)',
'GMT-06:00, Central (Regina)',
'GMT-05:00, S. America Pacific (Bogota)',
'GMT-05:00, Eastern (New York)',
'GMT-05:00, Eastern (Indiana)',
'GMT-04:00, Atlantic (Halifax)',
'GMT-04:00, S. America Western (Caracas)',
'GMT-03:30, Newfoundland (Newfoundland)',
'GMT-03:00, S. America Eastern (Brasilia)',
'GMT-03:00, S. America Eastern (Buenos Aires)',
'GMT-02:00, Mid-Atlantic (Mid-Atlantic)',
'GMT-01:00, Azores (Azores)',
'GMT+00:00, Greenwich (Casablanca)',
'GMT+00:00, GMT (London)',
'GMT+01:00, Europe (Amsterdam)',
'GMT+01:00, Europe (Paris)',
'GMT+01:00, Europe (Prague)',
'GMT+01:00, Europe (Berlin)',
'GMT+02:00, Greece (Athens)',
'GMT+02:00, Eastern Europe (Bucharest)',
'GMT+02:00, Egypt (Cairo)',
'GMT+02:00, South Africa (Pretoria)',
'GMT+02:00, Northern Europe (Helsinki)',
'GMT+02:00, Israel (Tel Aviv)',
'GMT+03:00, Saudi Arabia (Baghdad)',
'GMT+03:00, Russian (Moscow)',
'GMT+03:00, Nairobi (Nairobi)',
'GMT+03:00, Iran (Tehran)',
'GMT+04:00, Arabian (Abu Dhabi, Muscat)',
'GMT+04:00, Baku (Baku)',
'GMT+04:00, Afghanistan (Kabul)',
'GMT+05:00, West Asia (Ekaterinburg)',
'GMT+05:00, West Asia (Islamabad)',
'GMT+05:30, India (Bombay)',
'GMT+06:00, Columbo (Columbo)',
'GMT+06:00, Central Asia (Almaty)',
'GMT+07:00, Bangkok (Bangkok)',
'GMT+08:00, China (Beijing)',
'GMT+08:00, Australia Western (Perth)',
'GMT+08:00, Singapore (Singapore)',
'GMT+08:00, Taipei (Hong Kong)',
'GMT+09:00, Tokyo (Tokyo)',
'GMT+09:00, Korea (Seoul)',
'GMT+09:30, Yakutsk (Yakutsk)',
'GMT+09:30, Australia Central (Adelaide)',
'GMT+09:30, Australia Central (Darwin)',
'GMT+10:00, Australia Eastern (Brisbane)',
'GMT+10:00, Australia Eastern (Sydney)',
'GMT+10:00, West Pacific (Guam)',
'GMT+10:00, Tasmania (Hobart)',
'GMT+10:00, Vladivostok (Vladivostok)',
'GMT+11:00, Central Pacific (Solomon Is)',
'GMT+12:00, New Zealand (Wellington)',
'GMT+12:00, Fiji (Fiji)',
'GMT-09:00, Alaska (Anchorage)'
];
const gmtOffsets = [
'-12:00',
'-11:00',
'-10:00',
'-09:00',
'-08:00',
'-07:00',
'-07:00',
'-06:00',
'-06:00',
'-06:00',
'-05:00',
'-05:00',
'-05:00',
'-04:00',
'-04:00',
'-03:30',
'-03:00',
'-03:00',
'-02:00',
'-01:00',
'-09:00',
'00:00',
'00:00',
'01:00',
'01:00',
'01:00',
'01:00',
'02:00',
'02:00',
'02:00',
'02:00',
'02:00',
'02:00',
'03:00',
'03:00',
'03:00',
'03:00',
'04:00',
'04:00',
'04:00',
'05:00',
'05:00',
'05:30',
'06:00',
'06:00',
'07:00',
'08:00',
'08:00',
'08:00',
'08:00',
'09:00',
'09:00',
'09:30',
'09:30',
'09:30',
'10:00',
'10:00',
'10:00',
'10:00',
'10:00',
'11:00',
'12:00',
'12:00'
];
const pad = (num, size) => {
var sign = Math.sign(num) === -1 ? '-' : '';
return sign + new Array(size).concat([Math.abs(num)]).join('0').slice(-size);
};
const getIdByOffset = offset => {
let serializedOffset = offset;
if (typeof offset === 'number') {
if (offset > 12 || offset < -12) {
throw new Error('Not a valid offset, offset should be between -12 to 12');
} else {
serializedOffset = `${pad(offset, 2)}:00`;
}
}
if (typeof serializedOffset !== 'string') {
throw new Error(`Not a valid offset input, received ${typeof serializedOffset}`);
}
const isValidOffset = serializedOffset.match(/\d{2}:[0|3]0/);
if (!isValidOffset) {
throw new Error('Not a valid offset');
}
const timeZoneId = gmtOffsets.indexOf(serializedOffset);
if (timeZoneId === -1) {
throw new Error('Not valid offset found');
} else {
return timeZoneId;
}
};
const getTimezone = index => {
const timezoneName = timeZones[index];
if (timezoneName === -1) {
throw new Error(`No matching timezone, based on index: ${index}`);
}
return timezoneName;
};
module.exports = {
getIdByOffset,
getTimezone
};