-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtz.js.in
345 lines (324 loc) · 13.4 KB
/
tz.js.in
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* tz.js - Library for working with timezones in JavaScript
* Written in 2011 by L. David Baron <[email protected]>
* To the extent possible under law, the author(s) have dedicated all
* copyright and related and neighboring rights to this software to the
* public domain worldwide. This software is distributed without any
* warranty.
*
* You should have received a copy of the CC0 Public Domain Dedication
* along with this software. If not, see
* <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
/* tz.js @@VERSION@@ ; tzdata @@TZDATA_VERSION@@ */
(function() {
"use strict";
var gZones = @@ZONES@@;
var gLinks = @@LINKS@@;
var is_leap = function(year) {
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
var gDaysInMonth = [ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
var gSecsPerDay = 86400;
var gSecsPerWeek = 604800;
var g_rule_main_re = /^((?:<[^<>]+>)|(?:[^\d:,+-]+))([+-])?(\d{1,2})(:(\d{1,2})(:(\d{1,2}))?)?(.*)/;
var g_rule_dst_re = /^((?:<[^<>]+>)|(?:[^\d:,+-]+))(([+-])?(\d{1,2})(:(\d{1,2})(:(\d{1,2}))?)?)?,([^,]*),([^,]*)$/;
var g_start_end_re = /^([^\/]+)(\/([+-]?\d{1,3})(:(\d{1,2})(:(\d{1,2}))?)?)?$/;
var g_mwd_re = /^M(\d+)\.(\d+)\.(\d+)$/;
var numorz = function(str_or_undefined) {
if (typeof(str_or_undefined) == "undefined") {
return 0;
}
return Number(str_or_undefined);
}
var compute_rule_boundary = function(year, boundary, utc_offset_before) {
var m = boundary.match(g_start_end_re);
var daystr = m[1];
var time;
if (typeof(m[2]) == "undefined") {
// time is implicitly 2am local time
time = 7200;
} else {
time = 3600 * Number(m[3]) + 60 * numorz(m[5]) + numorz(m[7]);
}
// Based on daystr, set |d| to the start of the relevant day in UTC.
var d;
if (daystr[0] == "M") {
m = daystr.match(g_mwd_re);
var month = Number(m[1]);
var week = Number(m[2]);
var weekday = Number(m[3]);
d = Date.UTC(year, month-1) / 1000;
if (week == 5) {
d += (gDaysInMonth[month] - 7) * gSecsPerDay;
} else {
d += (week - 1) * gSecsPerWeek;
}
var weekstartday = (new Date(d * 1000)).getUTCDay();
d += ((weekday - weekstartday + 7) % 7) * gSecsPerDay
} else if (daystr[0] == "J") {
// Julian day, 1-365, never counting Feb. 29
// This means 59 is Feb 28, 60 is Mar 1.
var n = Number(daystr.substr(1));
var j = n - 1 + (n > 59 && is_leap(year));
d = Date.UTC(year, 0) / 1000 + j * gSecsPerDay;
} else {
// Julian day, 0-365, counting Feb. 29 when leap year
var j = Number(daystr);
d = Date.UTC(year, 0) / 1000 + j * gSecsPerDay;
}
return d + time - utc_offset_before;
}
var zone_pair_from_rule = function(rule, d) {
// See http://www.gnu.org/s/hello/manual/libc/TZ-Variable.html
// and the extensions documented in the man page for tzfile(5)
// in the time zone database distribution.
var m = rule.match(g_rule_main_re);
var abbr = m[1];
// In POSIX TZ format, UTC offsets are backwards.
var offset = (3600 * Number(m[3]) + 60 * numorz(m[5]) + numorz(m[7])) *
((m[2] == "-") ? 1 : -1);
var dst_part = m[8];
if (dst_part != "") {
m = dst_part.match(g_rule_dst_re);
var dst_abbr = m[1];
var dst_offset;
if (typeof(m[2]) == "undefined") {
// Optional offset omitted, thus implicitly 1 hour from norm.
dst_offset = offset + 3600;
} else {
dst_offset =
(3600 * Number(m[4]) + 60 * numorz(m[6]) + numorz(m[8])) *
((m[3] == "-") ? 1 : -1);
}
// As of version three of the timezone file format, the
// rules allow an extension in which daylight savings time
// may be indicated as being present all year, by use of a
// transition to daylight savings time at midnight local
// time on January 1, and a transition off of it at 25:00
// local time on December 31. This means as long as we use
// one of offset and dst_offset to pick the year to use,
// even if it's wrong (as it is in the last hour of the year
// in such cases), we'll still get the correct result. (We
// do need to be consistent because we need the same year
// for both computations; otherwise can pick start and end
// in different years and get DST wrong.)
var year = (new Date((d + offset) * 1000)).getUTCFullYear();
var start = compute_rule_boundary(year, m[9], offset);
var end = compute_rule_boundary(year, m[10], dst_offset);
var isdst = ((d < start) ^ (d < end)) ^ (end < start);
if (isdst) {
abbr = dst_abbr;
offset = dst_offset;
}
}
if (abbr[0] == "<" && abbr[abbr.length-1] == ">") {
// Trim the < and >, since they are not present for the
// non-rule case.
abbr = abbr.slice(1, -1);
}
return { offset: offset, abbr: abbr };
}
var zone_pair_from_table = function(zi, d) {
// binary search the list of times
var min = 0, max = zi.times.length - 1; // both inclusive
do {
var idx = min + Math.floor((max - min + 1) / 2);
if (d < zi.times[idx]) {
max = idx - 1;
} else {
min = idx;
}
} while (min != max);
var lttype = zi.types[zi.ltidx[min]];
return { offset: lttype.o, abbr: lttype.a };
}
var public_zoneAt = function(zone, dateObj) {
var czone = public_canonicalName(zone);
if (!czone) {
throw "Invalid time zone name: " + zone;
}
var zi = gZones[czone];
var d = dateObj.valueOf() / 1000;
if (d < 0) {
throw "Dates prior to 1970 not allowed.";
}
// per tzfile(5), the rule, if it exists, is for times after the
// last transition in the file
if (d >= zi.times[zi.times.length-1] && zi.rule) {
return zone_pair_from_rule(zi.rule, d);
}
return zone_pair_from_table(zi, d);
}
var public_datesFor = function(zone, year, month, day, hour, minute,
second) {
// Assume that zones do not change time more than once in any 48
// hour period, and that no zone is more than 24 hours from UTC.
//
// If those assumptions are false, we might fail to return a
// result that we should have returned, but
// make_and_check_result ensures we'll never return an incorrect
// one.
var du = Date.UTC(year, month-1, day, hour, minute, second) / 1000;
var d1 = du - gSecsPerDay;
var d2 = du + gSecsPerDay;
var z1 = public_zoneAt(zone, new Date(d1 * 1000));
var z2 = public_zoneAt(zone, new Date(d2 * 1000));
var results = [];
var make_and_check_result = function(offset) {
var d = du - offset; // our hypothetical date
var date = new Date(d * 1000);
var zi = public_zoneAt(zone, date);
if (zi.offset == offset) {
zi.date = date;
results.push(zi);
}
}
make_and_check_result(z1.offset);
if (z1.offset != z2.offset) {
make_and_check_result(z2.offset);
}
return results;
}
var gAllZones = null;
var public_allZones = function() {
if (!gAllZones) {
var zones = [];
for (var zone in gZones) {
zones.push(zone);
}
zones.sort();
gAllZones = zones;
}
return gAllZones;
}
var public_links = function() {
return gLinks;
}
var public_canonicalName = function(name) {
if (name in gZones) {
return name;
}
return gLinks[name]; // Returns undefined if not present
}
// Exports:
var tz_export = {
/**
* zoneAt(zone, dateObj)
*
* Given a valid named time zone (either a canonical zone name
* or an alias/link) and a JavaScript date object, return a zone
* object with two properties:
* offset: the UTC offset for the zone, in seconds
* abbr: the correct short name for the zone
*
* For example,
* tz.zoneAt("America/Los_Angeles",
* new Date(Date.UTC(1976,6,4,18,0,0)))
* (the time there is 1976-07-04 18:00:00 UTC) should return
* an object like this:
* { offset: -25200, abbr: "PDT" }
* since the America/Los_Angeles time zone observed summer time
* at the time given, and was thus at offset UTC minus 7 hours
* (or 25200 seconds), and used the abbreviation PDT (for
* Pacific Daylight Time).
*/
zoneAt: public_zoneAt,
/**
* datesFor(zone, year, month, day, hour, minute, second)
*
* Given a valid named time zone (either a canonical zone name
* or an alias/link) and a local time with:
* year >= 1970 (but not on January 1, 1970)
* month 1-12 (different from JS months, which are 0-11!)
* day 1-31
* hour 0-23
* minute 0-59
* second 0-59
* returns an *array* of objects with three properties, the two
* described above for zoneAt, and then a "date" property with a
* JavaScript date object. This array may have 0, 1, or 2
* elements (and in theory it may have more in the future,
* though the current code never produces such results).
*
* For example,
* tz.datesFor("America/Los_Angeles", 2011, 1, 1, 0, 0, 0)
* will produce one result:
* [
* { offset: -28800, abbr: "PST", date: (new Date(1293868800000)) }
* ]
* representing the moment of the new year 2011 in Los Angeles.
* This is the common case.
*
* However, around summer time changes zero or two results can
* occur. For example:
* tz.datesFor("America/Los_Angeles", 2011, 3, 13, 2, 30, 0)
* will produce an empty array since the start of summer time
* causes the clock to jump from 2011-03-13 01:59:59 PST to
* 2011-03-13 03:00:00 PDT.
*
* But this example:
* tz.datesFor("America/Los_Angeles", 2011, 11, 6, 1, 30, 0)
* will produce two results:
* [
* { offset: -25200, abbr: "PDT", date: (new Date(1320568200000)) }
* { offset: -28800, abbr: "PST", date: (new Date(1320571800000)) }
* ]
* representing times one hour apart, since the summer time
* change makes the clock jump from 2011-11-06 01:59:59 PDT to
* 2011-11-06 01:00:00 PST, and thus it passes 01:30 twice that
* day.
*/
datesFor: public_datesFor,
/**
* Return an array listing all of the canonical supported named
* time zones, in alphabetical order. The same array will be
* returned from each call.
*
* This is mainly useful if you want to enumerate all supported
* zones.
*/
allZones: public_allZones,
/**
* Return a hash mapping all aliased zone names (links) to their
* canonical zone names. The same object will be returned from
* each call.
*
* You will probably find the canonicalName function below more
* useful.
*/
links: public_links,
/**
* Return the canonical name of a timezone identifier that may
* or may not be an link (alias).
*
* Note that if the timezone database has the given zone as both
* a zone *and* a link, the zone is preferred.
*
* Returns undefined if there is no canonical name.
*
* For example, tz.canonicalName("America/Los_Angeles") will
* return "America/Los_Angeles", since America/Los_Angeles is a
* canonical timezone name. However,
* tz.canonicalName("US/Pacific") will also return
* "America/Los_Angeles" since US/Pacific is an alias for
* America/Los_Angeles. Likewise,
* tz.canonicalName("Africa/Addis_Ababa") will return
* "Africa/Nairobi" since the tz database no longer follows the
* convention that there is one canonical name per country, and
* has therefore made Africa/Addis_Ababa an alias for
* Africa/Nairobi (since both have followed the same time zone
* rules since 1970).
*
* This is useful if you want to canonicalize the timezone
* names. However, it is not required for use of tz.zoneAt or
* tz.datesFor.
*/
canonicalName: public_canonicalName,
};
if (typeof module !== "undefined" && module.exports) {
module.exports = tz_export;
} else {
window.tz = tz_export;
}
})();