forked from moment/moment-timezone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moment-timezone.js
528 lines (440 loc) · 11.6 KB
/
moment-timezone.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
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
// moment-timezone.js
// version : 0.0.3
// author : Tim Wood
// license : MIT
// github.com/timrwood/moment-timezone
(function () {
var VERSION = "0.0.3";
function onload(moment) {
var oldZoneName = moment.fn.zoneName,
oldZoneAbbr = moment.fn.zoneAbbr,
defaultRule,
rules = {},
ruleSets = {},
zones = {},
zoneSets = {},
links = {},
TIME_RULE_WALL_CLOCK = 0,
TIME_RULE_UTC = 1,
TIME_RULE_STANDARD = 2,
DAY_RULE_DAY_OF_MONTH = 7,
DAY_RULE_LAST_WEEKDAY = 8;
// converts time in the HH:mm:ss format to absolute number of minutes
function parseMinutes (input) {
input = input + '';
var output = input.split(':'),
sign = ~input.indexOf('-') ? -1 : 1,
hour = Math.abs(+output[0]),
minute = parseInt(output[1], 10) || 0,
second = parseInt(output[2], 10) || 0;
return sign * ((hour * 60) + (minute) + (second / 60));
}
/************************************
Rules
************************************/
function Rule (name, startYear, endYear, month, day, dayRule, time, timeRule, offset, letters) {
this.name = name;
this.startYear = +startYear;
this.endYear = +endYear;
this.month = +month;
this.day = +day;
this.dayRule = +dayRule;
this.time = parseMinutes(time);
this.timeRule = +timeRule;
this.offset = parseMinutes(offset);
this.letters = letters || '';
this.date = memoize(this.date);
this.weekdayAfter = memoize(this.weekdayAfter);
this.lastWeekday = memoize(this.lastWeekday);
}
Rule.prototype = {
contains : function (year) {
return (year >= this.startYear && year <= this.endYear);
},
start : function (year) {
year = Math.min(Math.max(year, this.startYear), this.endYear);
return moment.utc([year, this.month, this.date(year), 0, this.time]);
},
date : function (year) {
if (this.dayRule === DAY_RULE_DAY_OF_MONTH) {
return this.day;
} else if (this.dayRule === DAY_RULE_LAST_WEEKDAY) {
return this.lastWeekday(year);
}
return this.weekdayAfter(year);
},
weekdayAfter : function (year) {
var day = this.day,
firstDayOfWeek = moment([year, this.month, 1]).day(),
output = this.dayRule + 1 - firstDayOfWeek;
while (output < day) {
output += 7;
}
return output;
},
lastWeekday : function (year) {
var day = this.day,
dow = day % 7,
lastDowOfMonth = moment([year, this.month + 1, 1]).day(),
daysInMonth = moment([year, this.month, 1]).daysInMonth(),
output = daysInMonth + (dow - (lastDowOfMonth - 1)) - (~~(day / 7) * 7);
if (dow >= lastDowOfMonth) {
output -= 7;
}
return output;
}
};
/************************************
Rule Year
************************************/
function RuleYear (year, rule) {
this.rule = rule;
this.start = rule.start(year);
}
RuleYear.prototype = {
equals : function (other) {
if (!other || other.rule !== this.rule) {
return false;
}
return Math.abs(other.start - this.start) < 86400000; // 24 * 60 * 60 * 1000
}
};
function sortRuleYears (a, b) {
if (a.isLast) {
return -1;
}
if (b.isLast) {
return 1;
}
return b.start - a.start;
}
/************************************
Rule Sets
************************************/
function RuleSet (name) {
this.name = name;
this.rules = [];
this.lastYearRule = memoize(this.lastYearRule);
}
RuleSet.prototype = {
add : function (rule) {
this.rules.push(rule);
},
ruleYears : function (mom, lastZone) {
var i, j,
year = mom.year(),
rule,
lastZoneRule,
rules = [];
for (i = 0; i < this.rules.length; i++) {
rule = this.rules[i];
if (rule.contains(year)) {
rules.push(new RuleYear(year, rule));
} else if (rule.contains(year + 1)) {
rules.push(new RuleYear(year + 1, rule));
}
}
rules.push(new RuleYear(year - 1, this.lastYearRule(year - 1)));
if (lastZone) {
lastZoneRule = new RuleYear(year - 1, lastZone.lastRule());
lastZoneRule.start = lastZone.until.clone().utc();
lastZoneRule.isLast = lastZone.ruleSet !== this;
rules.push(lastZoneRule);
}
rules.sort(sortRuleYears);
return rules;
},
rule : function (mom, offset, lastZone) {
var rules = this.ruleYears(mom, lastZone),
lastOffset = 0,
rule,
lastZoneOffset,
lastZoneOffsetAbs,
lastRule,
i;
if (lastZone) {
lastZoneOffset = lastZone.offset + lastZone.lastRule().offset;
lastZoneOffsetAbs = Math.abs(lastZoneOffset) * 90000;
}
// make sure to include the previous rule's offset
for (i = rules.length - 1; i > -1; i--) {
lastRule = rule;
rule = rules[i];
if (rule.equals(lastRule)) {
continue;
}
if (lastZone && !rule.isLast && Math.abs(rule.start - lastZone.until) <= lastZoneOffsetAbs) {
lastOffset += lastZoneOffset - offset;
}
if (rule.rule.timeRule === TIME_RULE_STANDARD) {
lastOffset = offset;
}
if (rule.rule.timeRule !== TIME_RULE_UTC) {
rule.start.add('m', -lastOffset);
}
lastOffset = rule.rule.offset + offset;
}
for (i = 0; i < rules.length; i++) {
rule = rules[i];
if (mom >= rule.start && !rule.isLast) {
return rule.rule;
}
}
return defaultRule;
},
lastYearRule : function (year) {
var i,
rule,
start,
bestRule = defaultRule,
largest = -1e30;
for (i = 0; i < this.rules.length; i++) {
rule = this.rules[i];
if (year >= rule.startYear) {
start = rule.start(year);
if (start > largest) {
largest = start;
bestRule = rule;
}
}
}
return bestRule;
}
};
/************************************
Zone
************************************/
function Zone (name, offset, ruleSet, letters, until, untilOffset) {
var i,
untilArray = typeof until === 'string' ? until.split('_') : [9999];
this.name = name;
this.offset = parseMinutes(offset);
this.ruleSet = ruleSet;
this.letters = letters;
this.lastRule = memoize(this.lastRule);
for (i = 0; i < untilArray.length; i++) {
untilArray[i] = +untilArray[i];
}
this.until = moment.utc(untilArray).subtract('m', parseMinutes(untilOffset));
}
Zone.prototype = {
rule : function (mom, lastZone) {
return this.ruleSet.rule(mom, this.offset, lastZone);
},
lastRule : function () {
return this.rule(this.until);
},
format : function (rule) {
return this.letters.replace("%s", rule.letters);
}
};
/************************************
Zone Set
************************************/
function sortZones (a, b) {
return a.until - b.until;
}
function ZoneSet (name) {
this.name = normalizeName(name);
this.displayName = name;
this.zones = [];
this.zoneAndRule = memoize(this.zoneAndRule, function (mom) {
return +mom;
});
}
ZoneSet.prototype = {
zoneAndRule : function (mom) {
var i,
zone,
lastZone;
mom = mom.clone().utc();
for (i = 0; i < this.zones.length; i++) {
zone = this.zones[i];
if (mom < zone.until) {
break;
}
lastZone = zone;
}
return [zone, zone.rule(mom, lastZone)];
},
add : function (zone) {
this.zones.push(zone);
this.zones.sort(sortZones);
},
format : function (mom) {
var zoneAndRule = this.zoneAndRule(mom);
return zoneAndRule[0].format(zoneAndRule[1]);
},
offset : function (mom) {
var zoneAndRule = this.zoneAndRule(mom);
return -(zoneAndRule[0].offset + zoneAndRule[1].offset);
}
};
/************************************
Global Methods
************************************/
function memoize (fn, keyFn) {
var cache = {};
return function (first) {
var key = keyFn ? keyFn.apply(this, arguments) : first;
return key in cache ?
cache[key] :
(cache[key] = fn.apply(this, arguments));
};
}
function addRules (rules) {
var i, j, rule;
for (i in rules) {
rule = rules[i];
for (j = 0; j < rule.length; j++) {
addRule(i + '\t' + rule[j]);
}
}
}
function addRule (ruleString) {
// don't duplicate rules
if (rules[ruleString]) {
return rules[ruleString];
}
var p = ruleString.split(/\s/),
name = normalizeName(p[0]),
rule = new Rule(name, p[1], p[2], p[3], p[4], p[5], p[6], p[7], p[8], p[9], p[10]);
// cache the rule so we don't add it again
rules[ruleString] = rule;
// add to the ruleset
getRuleSet(name).add(rule);
return rule;
}
function normalizeName (name) {
return (name || '').toLowerCase().replace(/\//g, '_');
}
function addZones (zones) {
var i, j, zone;
for (i in zones) {
zone = zones[i];
for (j = 0; j < zone.length; j++) {
addZone(i + '\t' + zone[j]);
}
}
}
function addLinks (linksToAdd) {
var i;
for (i in linksToAdd) {
links[normalizeName(i)] = normalizeName(linksToAdd[i]);
}
}
function addZone (zoneString) {
// don't duplicate zones
if (zones[zoneString]) {
return zones[zoneString];
}
var p = zoneString.split(/\s/),
name = normalizeName(p[0]),
zone = new Zone(name, p[1], getRuleSet(p[2]), p[3], p[4], p[5]);
// cache the zone so we don't add it again
zones[zoneString] = zone;
// add to the zoneset
getZoneSet(p[0]).add(zone);
return zone;
}
function getRuleSet (name) {
name = normalizeName(name);
if (!ruleSets[name]) {
ruleSets[name] = new RuleSet(name);
}
return ruleSets[name];
}
function getZoneSet (name) {
var machineName = normalizeName(name);
if (links[machineName]) {
machineName = links[machineName];
}
if (!zoneSets[machineName]) {
zoneSets[machineName] = new ZoneSet(name);
}
return zoneSets[machineName];
}
function add (data) {
if (!data) {
return;
}
if (data.zones) {
addZones(data.zones);
}
if (data.rules) {
addRules(data.rules);
}
if (data.links) {
addLinks(data.links);
}
}
// overwrite moment.updateOffset
moment.updateOffset = function (mom, keepTime) {
var offset;
if (mom._z) {
offset = mom._z.offset(mom);
if (Math.abs(offset) < 16) {
offset = offset / 60;
}
mom.zone(offset, keepTime);
}
};
function getZoneSets() {
var sets = [],
zoneName;
for (zoneName in zoneSets) {
sets.push(zoneSets[zoneName]);
}
return sets;
}
moment.fn.tz = function (name) {
if (name) {
this._z = getZoneSet(name);
if (this._z) {
moment.updateOffset(this);
}
return this;
}
if (this._z) {
return this._z.displayName;
}
};
moment.fn.zoneName = function () {
if (this._z) {
return this._z.format(this);
}
return oldZoneName.call(this);
};
moment.fn.zoneAbbr = function () {
if (this._z) {
return this._z.format(this);
}
return oldZoneAbbr.call(this);
};
moment.tz = function () {
var args = [], i, len = arguments.length - 1;
for (i = 0; i < len; i++) {
args[i] = arguments[i];
}
var m = moment.apply(null, args);
var preTzOffset = m.zone();
m.tz(arguments[len]);
return m.add('minutes', m.zone() - preTzOffset);
};
moment.tz.add = add;
moment.tz.addRule = addRule;
moment.tz.addZone = addZone;
moment.tz.zones = getZoneSets;
moment.tz.version = VERSION;
// add default rule
defaultRule = addRule("- 0 9999 0 0 0 0 0 0");
return moment;
}
if (typeof define === "function" && define.amd) {
define("moment-timezone", ["moment"], onload);
} else if (typeof module !== 'undefined') {
module.exports = onload(require('moment'));
} else if (typeof window !== "undefined" && window.moment) {
onload(window.moment);
}
}).apply(this);