1 | //! moment-timezone.js |
---|
2 | //! version : 0.4.0 |
---|
3 | //! author : Tim Wood |
---|
4 | //! license : MIT |
---|
5 | //! github.com/moment/moment-timezone |
---|
6 | |
---|
7 | (function (root, factory) { |
---|
8 | "use strict"; |
---|
9 | |
---|
10 | /*global define*/ |
---|
11 | if (typeof define === 'function' && define.amd) { |
---|
12 | define(['moment'], factory); // AMD |
---|
13 | } else if (typeof exports === 'object') { |
---|
14 | module.exports = factory(require('moment')); // Node |
---|
15 | } else { |
---|
16 | factory(root.moment); // Browser |
---|
17 | } |
---|
18 | }(this, function (moment) { |
---|
19 | "use strict"; |
---|
20 | |
---|
21 | // Do not load moment-timezone a second time. |
---|
22 | if (moment.tz !== undefined) { |
---|
23 | logError('Moment Timezone ' + moment.tz.version + ' was already loaded ' + (moment.tz.dataVersion ? 'with data from ' : 'without any data') + moment.tz.dataVersion); |
---|
24 | return moment; |
---|
25 | } |
---|
26 | |
---|
27 | var VERSION = "0.4.0", |
---|
28 | zones = {}, |
---|
29 | links = {}, |
---|
30 | names = {}, |
---|
31 | |
---|
32 | momentVersion = moment.version.split('.'), |
---|
33 | major = +momentVersion[0], |
---|
34 | minor = +momentVersion[1]; |
---|
35 | |
---|
36 | // Moment.js version check |
---|
37 | if (major < 2 || (major === 2 && minor < 6)) { |
---|
38 | logError('Moment Timezone requires Moment.js >= 2.6.0. You are using Moment.js ' + moment.version + '. See momentjs.com'); |
---|
39 | } |
---|
40 | |
---|
41 | /************************************ |
---|
42 | Unpacking |
---|
43 | ************************************/ |
---|
44 | |
---|
45 | function charCodeToInt(charCode) { |
---|
46 | if (charCode > 96) { |
---|
47 | return charCode - 87; |
---|
48 | } else if (charCode > 64) { |
---|
49 | return charCode - 29; |
---|
50 | } |
---|
51 | return charCode - 48; |
---|
52 | } |
---|
53 | |
---|
54 | function unpackBase60(string) { |
---|
55 | var i = 0, |
---|
56 | parts = string.split('.'), |
---|
57 | whole = parts[0], |
---|
58 | fractional = parts[1] || '', |
---|
59 | multiplier = 1, |
---|
60 | num, |
---|
61 | out = 0, |
---|
62 | sign = 1; |
---|
63 | |
---|
64 | // handle negative numbers |
---|
65 | if (string.charCodeAt(0) === 45) { |
---|
66 | i = 1; |
---|
67 | sign = -1; |
---|
68 | } |
---|
69 | |
---|
70 | // handle digits before the decimal |
---|
71 | for (i; i < whole.length; i++) { |
---|
72 | num = charCodeToInt(whole.charCodeAt(i)); |
---|
73 | out = 60 * out + num; |
---|
74 | } |
---|
75 | |
---|
76 | // handle digits after the decimal |
---|
77 | for (i = 0; i < fractional.length; i++) { |
---|
78 | multiplier = multiplier / 60; |
---|
79 | num = charCodeToInt(fractional.charCodeAt(i)); |
---|
80 | out += num * multiplier; |
---|
81 | } |
---|
82 | |
---|
83 | return out * sign; |
---|
84 | } |
---|
85 | |
---|
86 | function arrayToInt (array) { |
---|
87 | for (var i = 0; i < array.length; i++) { |
---|
88 | array[i] = unpackBase60(array[i]); |
---|
89 | } |
---|
90 | } |
---|
91 | |
---|
92 | function intToUntil (array, length) { |
---|
93 | for (var i = 0; i < length; i++) { |
---|
94 | array[i] = Math.round((array[i - 1] || 0) + (array[i] * 60000)); // minutes to milliseconds |
---|
95 | } |
---|
96 | |
---|
97 | array[length - 1] = Infinity; |
---|
98 | } |
---|
99 | |
---|
100 | function mapIndices (source, indices) { |
---|
101 | var out = [], i; |
---|
102 | |
---|
103 | for (i = 0; i < indices.length; i++) { |
---|
104 | out[i] = source[indices[i]]; |
---|
105 | } |
---|
106 | |
---|
107 | return out; |
---|
108 | } |
---|
109 | |
---|
110 | function unpack (string) { |
---|
111 | var data = string.split('|'), |
---|
112 | offsets = data[2].split(' '), |
---|
113 | indices = data[3].split(''), |
---|
114 | untils = data[4].split(' '); |
---|
115 | |
---|
116 | arrayToInt(offsets); |
---|
117 | arrayToInt(indices); |
---|
118 | arrayToInt(untils); |
---|
119 | |
---|
120 | intToUntil(untils, indices.length); |
---|
121 | |
---|
122 | return { |
---|
123 | name : data[0], |
---|
124 | abbrs : mapIndices(data[1].split(' '), indices), |
---|
125 | offsets : mapIndices(offsets, indices), |
---|
126 | untils : untils |
---|
127 | }; |
---|
128 | } |
---|
129 | |
---|
130 | /************************************ |
---|
131 | Zone object |
---|
132 | ************************************/ |
---|
133 | |
---|
134 | function Zone (packedString) { |
---|
135 | if (packedString) { |
---|
136 | this._set(unpack(packedString)); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | Zone.prototype = { |
---|
141 | _set : function (unpacked) { |
---|
142 | this.name = unpacked.name; |
---|
143 | this.abbrs = unpacked.abbrs; |
---|
144 | this.untils = unpacked.untils; |
---|
145 | this.offsets = unpacked.offsets; |
---|
146 | }, |
---|
147 | |
---|
148 | _index : function (timestamp) { |
---|
149 | var target = +timestamp, |
---|
150 | untils = this.untils, |
---|
151 | i; |
---|
152 | |
---|
153 | for (i = 0; i < untils.length; i++) { |
---|
154 | if (target < untils[i]) { |
---|
155 | return i; |
---|
156 | } |
---|
157 | } |
---|
158 | }, |
---|
159 | |
---|
160 | parse : function (timestamp) { |
---|
161 | var target = +timestamp, |
---|
162 | offsets = this.offsets, |
---|
163 | untils = this.untils, |
---|
164 | max = untils.length - 1, |
---|
165 | offset, offsetNext, offsetPrev, i; |
---|
166 | |
---|
167 | for (i = 0; i < max; i++) { |
---|
168 | offset = offsets[i]; |
---|
169 | offsetNext = offsets[i + 1]; |
---|
170 | offsetPrev = offsets[i ? i - 1 : i]; |
---|
171 | |
---|
172 | if (offset < offsetNext && tz.moveAmbiguousForward) { |
---|
173 | offset = offsetNext; |
---|
174 | } else if (offset > offsetPrev && tz.moveInvalidForward) { |
---|
175 | offset = offsetPrev; |
---|
176 | } |
---|
177 | |
---|
178 | if (target < untils[i] - (offset * 60000)) { |
---|
179 | return offsets[i]; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | return offsets[max]; |
---|
184 | }, |
---|
185 | |
---|
186 | abbr : function (mom) { |
---|
187 | return this.abbrs[this._index(mom)]; |
---|
188 | }, |
---|
189 | |
---|
190 | offset : function (mom) { |
---|
191 | return this.offsets[this._index(mom)]; |
---|
192 | } |
---|
193 | }; |
---|
194 | |
---|
195 | /************************************ |
---|
196 | Global Methods |
---|
197 | ************************************/ |
---|
198 | |
---|
199 | function normalizeName (name) { |
---|
200 | return (name || '').toLowerCase().replace(/\//g, '_'); |
---|
201 | } |
---|
202 | |
---|
203 | function addZone (packed) { |
---|
204 | var i, name, normalized; |
---|
205 | |
---|
206 | if (typeof packed === "string") { |
---|
207 | packed = [packed]; |
---|
208 | } |
---|
209 | |
---|
210 | for (i = 0; i < packed.length; i++) { |
---|
211 | name = packed[i].split('|')[0]; |
---|
212 | normalized = normalizeName(name); |
---|
213 | zones[normalized] = packed[i]; |
---|
214 | names[normalized] = name; |
---|
215 | } |
---|
216 | } |
---|
217 | |
---|
218 | function getZone (name, caller) { |
---|
219 | name = normalizeName(name); |
---|
220 | |
---|
221 | var zone = zones[name]; |
---|
222 | var link; |
---|
223 | |
---|
224 | if (zone instanceof Zone) { |
---|
225 | return zone; |
---|
226 | } |
---|
227 | |
---|
228 | if (typeof zone === 'string') { |
---|
229 | zone = new Zone(zone); |
---|
230 | zones[name] = zone; |
---|
231 | return zone; |
---|
232 | } |
---|
233 | |
---|
234 | // Pass getZone to prevent recursion more than 1 level deep |
---|
235 | if (links[name] && caller !== getZone && (link = getZone(links[name], getZone))) { |
---|
236 | zone = zones[name] = new Zone(); |
---|
237 | zone._set(link); |
---|
238 | zone.name = names[name]; |
---|
239 | return zone; |
---|
240 | } |
---|
241 | |
---|
242 | return null; |
---|
243 | } |
---|
244 | |
---|
245 | function getNames () { |
---|
246 | var i, out = []; |
---|
247 | |
---|
248 | for (i in names) { |
---|
249 | if (names.hasOwnProperty(i) && (zones[i] || zones[links[i]]) && names[i]) { |
---|
250 | out.push(names[i]); |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | return out.sort(); |
---|
255 | } |
---|
256 | |
---|
257 | function addLink (aliases) { |
---|
258 | var i, alias, normal0, normal1; |
---|
259 | |
---|
260 | if (typeof aliases === "string") { |
---|
261 | aliases = [aliases]; |
---|
262 | } |
---|
263 | |
---|
264 | for (i = 0; i < aliases.length; i++) { |
---|
265 | alias = aliases[i].split('|'); |
---|
266 | |
---|
267 | normal0 = normalizeName(alias[0]); |
---|
268 | normal1 = normalizeName(alias[1]); |
---|
269 | |
---|
270 | links[normal0] = normal1; |
---|
271 | names[normal0] = alias[0]; |
---|
272 | |
---|
273 | links[normal1] = normal0; |
---|
274 | names[normal1] = alias[1]; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | function loadData (data) { |
---|
279 | addZone(data.zones); |
---|
280 | addLink(data.links); |
---|
281 | tz.dataVersion = data.version; |
---|
282 | } |
---|
283 | |
---|
284 | function zoneExists (name) { |
---|
285 | if (!zoneExists.didShowError) { |
---|
286 | zoneExists.didShowError = true; |
---|
287 | logError("moment.tz.zoneExists('" + name + "') has been deprecated in favor of !moment.tz.zone('" + name + "')"); |
---|
288 | } |
---|
289 | return !!getZone(name); |
---|
290 | } |
---|
291 | |
---|
292 | function needsOffset (m) { |
---|
293 | return !!(m._a && (m._tzm === undefined)); |
---|
294 | } |
---|
295 | |
---|
296 | function logError (message) { |
---|
297 | if (typeof console !== 'undefined' && typeof console.error === 'function') { |
---|
298 | console.error(message); |
---|
299 | } |
---|
300 | } |
---|
301 | |
---|
302 | /************************************ |
---|
303 | moment.tz namespace |
---|
304 | ************************************/ |
---|
305 | |
---|
306 | function tz (input) { |
---|
307 | var args = Array.prototype.slice.call(arguments, 0, -1), |
---|
308 | name = arguments[arguments.length - 1], |
---|
309 | zone = getZone(name), |
---|
310 | out = moment.utc.apply(null, args); |
---|
311 | |
---|
312 | if (zone && !moment.isMoment(input) && needsOffset(out)) { |
---|
313 | out.add(zone.parse(out), 'minutes'); |
---|
314 | } |
---|
315 | |
---|
316 | out.tz(name); |
---|
317 | |
---|
318 | return out; |
---|
319 | } |
---|
320 | |
---|
321 | tz.version = VERSION; |
---|
322 | tz.dataVersion = ''; |
---|
323 | tz._zones = zones; |
---|
324 | tz._links = links; |
---|
325 | tz._names = names; |
---|
326 | tz.add = addZone; |
---|
327 | tz.link = addLink; |
---|
328 | tz.load = loadData; |
---|
329 | tz.zone = getZone; |
---|
330 | tz.zoneExists = zoneExists; // deprecated in 0.1.0 |
---|
331 | tz.names = getNames; |
---|
332 | tz.Zone = Zone; |
---|
333 | tz.unpack = unpack; |
---|
334 | tz.unpackBase60 = unpackBase60; |
---|
335 | tz.needsOffset = needsOffset; |
---|
336 | tz.moveInvalidForward = true; |
---|
337 | tz.moveAmbiguousForward = false; |
---|
338 | |
---|
339 | /************************************ |
---|
340 | Interface with Moment.js |
---|
341 | ************************************/ |
---|
342 | |
---|
343 | var fn = moment.fn; |
---|
344 | |
---|
345 | moment.tz = tz; |
---|
346 | |
---|
347 | moment.defaultZone = null; |
---|
348 | |
---|
349 | moment.updateOffset = function (mom, keepTime) { |
---|
350 | var zone = moment.defaultZone, |
---|
351 | offset; |
---|
352 | |
---|
353 | if (mom._z === undefined) { |
---|
354 | if (zone && needsOffset(mom) && !mom._isUTC) { |
---|
355 | mom._d = moment.utc(mom._a)._d; |
---|
356 | mom.utc().add(zone.parse(mom), 'minutes'); |
---|
357 | } |
---|
358 | mom._z = zone; |
---|
359 | } |
---|
360 | if (mom._z) { |
---|
361 | offset = mom._z.offset(mom); |
---|
362 | if (Math.abs(offset) < 16) { |
---|
363 | offset = offset / 60; |
---|
364 | } |
---|
365 | if (mom.utcOffset !== undefined) { |
---|
366 | mom.utcOffset(-offset, keepTime); |
---|
367 | } else { |
---|
368 | mom.zone(offset, keepTime); |
---|
369 | } |
---|
370 | } |
---|
371 | }; |
---|
372 | |
---|
373 | fn.tz = function (name) { |
---|
374 | if (name) { |
---|
375 | this._z = getZone(name); |
---|
376 | if (this._z) { |
---|
377 | moment.updateOffset(this); |
---|
378 | } else { |
---|
379 | logError("Moment Timezone has no data for " + name + ". See http://momentjs.com/timezone/docs/#/data-loading/."); |
---|
380 | } |
---|
381 | return this; |
---|
382 | } |
---|
383 | if (this._z) { return this._z.name; } |
---|
384 | }; |
---|
385 | |
---|
386 | function abbrWrap (old) { |
---|
387 | return function () { |
---|
388 | if (this._z) { return this._z.abbr(this); } |
---|
389 | return old.call(this); |
---|
390 | }; |
---|
391 | } |
---|
392 | |
---|
393 | function resetZoneWrap (old) { |
---|
394 | return function () { |
---|
395 | this._z = null; |
---|
396 | return old.apply(this, arguments); |
---|
397 | }; |
---|
398 | } |
---|
399 | |
---|
400 | fn.zoneName = abbrWrap(fn.zoneName); |
---|
401 | fn.zoneAbbr = abbrWrap(fn.zoneAbbr); |
---|
402 | fn.utc = resetZoneWrap(fn.utc); |
---|
403 | |
---|
404 | moment.tz.setDefault = function(name) { |
---|
405 | if (major < 2 || (major === 2 && minor < 9)) { |
---|
406 | logError('Moment Timezone setDefault() requires Moment.js >= 2.9.0. You are using Moment.js ' + moment.version + '.'); |
---|
407 | } |
---|
408 | moment.defaultZone = name ? getZone(name) : null; |
---|
409 | return moment; |
---|
410 | }; |
---|
411 | |
---|
412 | // Cloning a moment should include the _z property. |
---|
413 | var momentProperties = moment.momentProperties; |
---|
414 | if (Object.prototype.toString.call(momentProperties) === '[object Array]') { |
---|
415 | // moment 2.8.1+ |
---|
416 | momentProperties.push('_z'); |
---|
417 | momentProperties.push('_a'); |
---|
418 | } else if (momentProperties) { |
---|
419 | // moment 2.7.0 |
---|
420 | momentProperties._z = null; |
---|
421 | } |
---|
422 | |
---|
423 | // INJECT DATA |
---|
424 | |
---|
425 | return moment; |
---|
426 | })); |
---|