moment-timezone.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. //! moment-timezone.js
  2. //! version : 0.5.33
  3. //! Copyright (c) JS Foundation and other contributors
  4. //! license : MIT
  5. //! github.com/moment/moment-timezone
  6. (function (root, factory) {
  7. "use strict";
  8. /*global define*/
  9. if (typeof module === 'object' && module.exports) {
  10. module.exports = factory(require('moment')); // Node
  11. } else if (typeof define === 'function' && define.amd) {
  12. define(['moment'], factory); // AMD
  13. } else {
  14. factory(root.moment); // Browser
  15. }
  16. }(this, function (moment) {
  17. "use strict";
  18. // Resolves es6 module loading issue
  19. if (moment.version === undefined && moment.default) {
  20. moment = moment.default;
  21. }
  22. // Do not load moment-timezone a second time.
  23. // if (moment.tz !== undefined) {
  24. // logError('Moment Timezone ' + moment.tz.version + ' was already loaded ' + (moment.tz.dataVersion ? 'with data from ' : 'without any data') + moment.tz.dataVersion);
  25. // return moment;
  26. // }
  27. var VERSION = "0.5.33",
  28. zones = {},
  29. links = {},
  30. countries = {},
  31. names = {},
  32. guesses = {},
  33. cachedGuess;
  34. if (!moment || typeof moment.version !== 'string') {
  35. logError('Moment Timezone requires Moment.js. See https://momentjs.com/timezone/docs/#/use-it/browser/');
  36. }
  37. var momentVersion = moment.version.split('.'),
  38. major = +momentVersion[0],
  39. minor = +momentVersion[1];
  40. // Moment.js version check
  41. if (major < 2 || (major === 2 && minor < 6)) {
  42. logError('Moment Timezone requires Moment.js >= 2.6.0. You are using Moment.js ' + moment.version + '. See momentjs.com');
  43. }
  44. /************************************
  45. Unpacking
  46. ************************************/
  47. function charCodeToInt(charCode) {
  48. if (charCode > 96) {
  49. return charCode - 87;
  50. } else if (charCode > 64) {
  51. return charCode - 29;
  52. }
  53. return charCode - 48;
  54. }
  55. function unpackBase60(string) {
  56. var i = 0,
  57. parts = string.split('.'),
  58. whole = parts[0],
  59. fractional = parts[1] || '',
  60. multiplier = 1,
  61. num,
  62. out = 0,
  63. sign = 1;
  64. // handle negative numbers
  65. if (string.charCodeAt(0) === 45) {
  66. i = 1;
  67. sign = -1;
  68. }
  69. // handle digits before the decimal
  70. for (i; i < whole.length; i++) {
  71. num = charCodeToInt(whole.charCodeAt(i));
  72. out = 60 * out + num;
  73. }
  74. // handle digits after the decimal
  75. for (i = 0; i < fractional.length; i++) {
  76. multiplier = multiplier / 60;
  77. num = charCodeToInt(fractional.charCodeAt(i));
  78. out += num * multiplier;
  79. }
  80. return out * sign;
  81. }
  82. function arrayToInt (array) {
  83. for (var i = 0; i < array.length; i++) {
  84. array[i] = unpackBase60(array[i]);
  85. }
  86. }
  87. function intToUntil (array, length) {
  88. for (var i = 0; i < length; i++) {
  89. array[i] = Math.round((array[i - 1] || 0) + (array[i] * 60000)); // minutes to milliseconds
  90. }
  91. array[length - 1] = Infinity;
  92. }
  93. function mapIndices (source, indices) {
  94. var out = [], i;
  95. for (i = 0; i < indices.length; i++) {
  96. out[i] = source[indices[i]];
  97. }
  98. return out;
  99. }
  100. function unpack (string) {
  101. var data = string.split('|'),
  102. offsets = data[2].split(' '),
  103. indices = data[3].split(''),
  104. untils = data[4].split(' ');
  105. arrayToInt(offsets);
  106. arrayToInt(indices);
  107. arrayToInt(untils);
  108. intToUntil(untils, indices.length);
  109. return {
  110. name : data[0],
  111. abbrs : mapIndices(data[1].split(' '), indices),
  112. offsets : mapIndices(offsets, indices),
  113. untils : untils,
  114. population : data[5] | 0
  115. };
  116. }
  117. /************************************
  118. Zone object
  119. ************************************/
  120. function Zone (packedString) {
  121. if (packedString) {
  122. this._set(unpack(packedString));
  123. }
  124. }
  125. Zone.prototype = {
  126. _set : function (unpacked) {
  127. this.name = unpacked.name;
  128. this.abbrs = unpacked.abbrs;
  129. this.untils = unpacked.untils;
  130. this.offsets = unpacked.offsets;
  131. this.population = unpacked.population;
  132. },
  133. _index : function (timestamp) {
  134. var target = +timestamp,
  135. untils = this.untils,
  136. i;
  137. for (i = 0; i < untils.length; i++) {
  138. if (target < untils[i]) {
  139. return i;
  140. }
  141. }
  142. },
  143. countries : function () {
  144. var zone_name = this.name;
  145. return Object.keys(countries).filter(function (country_code) {
  146. return countries[country_code].zones.indexOf(zone_name) !== -1;
  147. });
  148. },
  149. parse : function (timestamp) {
  150. var target = +timestamp,
  151. offsets = this.offsets,
  152. untils = this.untils,
  153. max = untils.length - 1,
  154. offset, offsetNext, offsetPrev, i;
  155. for (i = 0; i < max; i++) {
  156. offset = offsets[i];
  157. offsetNext = offsets[i + 1];
  158. offsetPrev = offsets[i ? i - 1 : i];
  159. if (offset < offsetNext && tz.moveAmbiguousForward) {
  160. offset = offsetNext;
  161. } else if (offset > offsetPrev && tz.moveInvalidForward) {
  162. offset = offsetPrev;
  163. }
  164. if (target < untils[i] - (offset * 60000)) {
  165. return offsets[i];
  166. }
  167. }
  168. return offsets[max];
  169. },
  170. abbr : function (mom) {
  171. return this.abbrs[this._index(mom)];
  172. },
  173. offset : function (mom) {
  174. logError("zone.offset has been deprecated in favor of zone.utcOffset");
  175. return this.offsets[this._index(mom)];
  176. },
  177. utcOffset : function (mom) {
  178. return this.offsets[this._index(mom)];
  179. }
  180. };
  181. /************************************
  182. Country object
  183. ************************************/
  184. function Country (country_name, zone_names) {
  185. this.name = country_name;
  186. this.zones = zone_names;
  187. }
  188. /************************************
  189. Current Timezone
  190. ************************************/
  191. function OffsetAt(at) {
  192. var timeString = at.toTimeString();
  193. var abbr = timeString.match(/\([a-z ]+\)/i);
  194. if (abbr && abbr[0]) {
  195. // 17:56:31 GMT-0600 (CST)
  196. // 17:56:31 GMT-0600 (Central Standard Time)
  197. abbr = abbr[0].match(/[A-Z]/g);
  198. abbr = abbr ? abbr.join('') : undefined;
  199. } else {
  200. // 17:56:31 CST
  201. // 17:56:31 GMT+0800 (台北標準時間)
  202. abbr = timeString.match(/[A-Z]{3,5}/g);
  203. abbr = abbr ? abbr[0] : undefined;
  204. }
  205. if (abbr === 'GMT') {
  206. abbr = undefined;
  207. }
  208. this.at = +at;
  209. this.abbr = abbr;
  210. this.offset = at.getTimezoneOffset();
  211. }
  212. function ZoneScore(zone) {
  213. this.zone = zone;
  214. this.offsetScore = 0;
  215. this.abbrScore = 0;
  216. }
  217. ZoneScore.prototype.scoreOffsetAt = function (offsetAt) {
  218. this.offsetScore += Math.abs(this.zone.utcOffset(offsetAt.at) - offsetAt.offset);
  219. if (this.zone.abbr(offsetAt.at).replace(/[^A-Z]/g, '') !== offsetAt.abbr) {
  220. this.abbrScore++;
  221. }
  222. };
  223. function findChange(low, high) {
  224. var mid, diff;
  225. while ((diff = ((high.at - low.at) / 12e4 | 0) * 6e4)) {
  226. mid = new OffsetAt(new Date(low.at + diff));
  227. if (mid.offset === low.offset) {
  228. low = mid;
  229. } else {
  230. high = mid;
  231. }
  232. }
  233. return low;
  234. }
  235. function userOffsets() {
  236. var startYear = new Date().getFullYear() - 2,
  237. last = new OffsetAt(new Date(startYear, 0, 1)),
  238. offsets = [last],
  239. change, next, i;
  240. for (i = 1; i < 48; i++) {
  241. next = new OffsetAt(new Date(startYear, i, 1));
  242. if (next.offset !== last.offset) {
  243. change = findChange(last, next);
  244. offsets.push(change);
  245. offsets.push(new OffsetAt(new Date(change.at + 6e4)));
  246. }
  247. last = next;
  248. }
  249. for (i = 0; i < 4; i++) {
  250. offsets.push(new OffsetAt(new Date(startYear + i, 0, 1)));
  251. offsets.push(new OffsetAt(new Date(startYear + i, 6, 1)));
  252. }
  253. return offsets;
  254. }
  255. function sortZoneScores (a, b) {
  256. if (a.offsetScore !== b.offsetScore) {
  257. return a.offsetScore - b.offsetScore;
  258. }
  259. if (a.abbrScore !== b.abbrScore) {
  260. return a.abbrScore - b.abbrScore;
  261. }
  262. if (a.zone.population !== b.zone.population) {
  263. return b.zone.population - a.zone.population;
  264. }
  265. return b.zone.name.localeCompare(a.zone.name);
  266. }
  267. function addToGuesses (name, offsets) {
  268. var i, offset;
  269. arrayToInt(offsets);
  270. for (i = 0; i < offsets.length; i++) {
  271. offset = offsets[i];
  272. guesses[offset] = guesses[offset] || {};
  273. guesses[offset][name] = true;
  274. }
  275. }
  276. function guessesForUserOffsets (offsets) {
  277. var offsetsLength = offsets.length,
  278. filteredGuesses = {},
  279. out = [],
  280. i, j, guessesOffset;
  281. for (i = 0; i < offsetsLength; i++) {
  282. guessesOffset = guesses[offsets[i].offset] || {};
  283. for (j in guessesOffset) {
  284. if (guessesOffset.hasOwnProperty(j)) {
  285. filteredGuesses[j] = true;
  286. }
  287. }
  288. }
  289. for (i in filteredGuesses) {
  290. if (filteredGuesses.hasOwnProperty(i)) {
  291. out.push(names[i]);
  292. }
  293. }
  294. return out;
  295. }
  296. function rebuildGuess () {
  297. // use Intl API when available and returning valid time zone
  298. try {
  299. var intlName = Intl.DateTimeFormat().resolvedOptions().timeZone;
  300. if (intlName && intlName.length > 3) {
  301. var name = names[normalizeName(intlName)];
  302. if (name) {
  303. return name;
  304. }
  305. logError("Moment Timezone found " + intlName + " from the Intl api, but did not have that data loaded.");
  306. }
  307. } catch (e) {
  308. // Intl unavailable, fall back to manual guessing.
  309. }
  310. var offsets = userOffsets(),
  311. offsetsLength = offsets.length,
  312. guesses = guessesForUserOffsets(offsets),
  313. zoneScores = [],
  314. zoneScore, i, j;
  315. for (i = 0; i < guesses.length; i++) {
  316. zoneScore = new ZoneScore(getZone(guesses[i]), offsetsLength);
  317. for (j = 0; j < offsetsLength; j++) {
  318. zoneScore.scoreOffsetAt(offsets[j]);
  319. }
  320. zoneScores.push(zoneScore);
  321. }
  322. zoneScores.sort(sortZoneScores);
  323. return zoneScores.length > 0 ? zoneScores[0].zone.name : undefined;
  324. }
  325. function guess (ignoreCache) {
  326. if (!cachedGuess || ignoreCache) {
  327. cachedGuess = rebuildGuess();
  328. }
  329. return cachedGuess;
  330. }
  331. /************************************
  332. Global Methods
  333. ************************************/
  334. function normalizeName (name) {
  335. return (name || '').toLowerCase().replace(/\//g, '_');
  336. }
  337. function addZone (packed) {
  338. var i, name, split, normalized;
  339. if (typeof packed === "string") {
  340. packed = [packed];
  341. }
  342. for (i = 0; i < packed.length; i++) {
  343. split = packed[i].split('|');
  344. name = split[0];
  345. normalized = normalizeName(name);
  346. zones[normalized] = packed[i];
  347. names[normalized] = name;
  348. addToGuesses(normalized, split[2].split(' '));
  349. }
  350. }
  351. function getZone (name, caller) {
  352. name = normalizeName(name);
  353. var zone = zones[name];
  354. var link;
  355. if (zone instanceof Zone) {
  356. return zone;
  357. }
  358. if (typeof zone === 'string') {
  359. zone = new Zone(zone);
  360. zones[name] = zone;
  361. return zone;
  362. }
  363. // Pass getZone to prevent recursion more than 1 level deep
  364. if (links[name] && caller !== getZone && (link = getZone(links[name], getZone))) {
  365. zone = zones[name] = new Zone();
  366. zone._set(link);
  367. zone.name = names[name];
  368. return zone;
  369. }
  370. return null;
  371. }
  372. function getNames () {
  373. var i, out = [];
  374. for (i in names) {
  375. if (names.hasOwnProperty(i) && (zones[i] || zones[links[i]]) && names[i]) {
  376. out.push(names[i]);
  377. }
  378. }
  379. return out.sort();
  380. }
  381. function getCountryNames () {
  382. return Object.keys(countries);
  383. }
  384. function addLink (aliases) {
  385. var i, alias, normal0, normal1;
  386. if (typeof aliases === "string") {
  387. aliases = [aliases];
  388. }
  389. for (i = 0; i < aliases.length; i++) {
  390. alias = aliases[i].split('|');
  391. normal0 = normalizeName(alias[0]);
  392. normal1 = normalizeName(alias[1]);
  393. links[normal0] = normal1;
  394. names[normal0] = alias[0];
  395. links[normal1] = normal0;
  396. names[normal1] = alias[1];
  397. }
  398. }
  399. function addCountries (data) {
  400. var i, country_code, country_zones, split;
  401. if (!data || !data.length) return;
  402. for (i = 0; i < data.length; i++) {
  403. split = data[i].split('|');
  404. country_code = split[0].toUpperCase();
  405. country_zones = split[1].split(' ');
  406. countries[country_code] = new Country(
  407. country_code,
  408. country_zones
  409. );
  410. }
  411. }
  412. function getCountry (name) {
  413. name = name.toUpperCase();
  414. return countries[name] || null;
  415. }
  416. function zonesForCountry(country, with_offset) {
  417. country = getCountry(country);
  418. if (!country) return null;
  419. var zones = country.zones.sort();
  420. if (with_offset) {
  421. return zones.map(function (zone_name) {
  422. var zone = getZone(zone_name);
  423. return {
  424. name: zone_name,
  425. offset: zone.utcOffset(new Date())
  426. };
  427. });
  428. }
  429. return zones;
  430. }
  431. function loadData (data) {
  432. addZone(data.zones);
  433. addLink(data.links);
  434. addCountries(data.countries);
  435. tz.dataVersion = data.version;
  436. }
  437. function zoneExists (name) {
  438. if (!zoneExists.didShowError) {
  439. zoneExists.didShowError = true;
  440. logError("moment.tz.zoneExists('" + name + "') has been deprecated in favor of !moment.tz.zone('" + name + "')");
  441. }
  442. return !!getZone(name);
  443. }
  444. function needsOffset (m) {
  445. var isUnixTimestamp = (m._f === 'X' || m._f === 'x');
  446. return !!(m._a && (m._tzm === undefined) && !isUnixTimestamp);
  447. }
  448. function logError (message) {
  449. if (typeof console !== 'undefined' && typeof console.error === 'function') {
  450. console.error(message);
  451. }
  452. }
  453. /************************************
  454. moment.tz namespace
  455. ************************************/
  456. function tz (input) {
  457. var args = Array.prototype.slice.call(arguments, 0, -1),
  458. name = arguments[arguments.length - 1],
  459. zone = getZone(name),
  460. out = moment.utc.apply(null, args);
  461. if (zone && !moment.isMoment(input) && needsOffset(out)) {
  462. out.add(zone.parse(out), 'minutes');
  463. }
  464. out.tz(name);
  465. return out;
  466. }
  467. tz.version = VERSION;
  468. tz.dataVersion = '';
  469. tz._zones = zones;
  470. tz._links = links;
  471. tz._names = names;
  472. tz._countries = countries;
  473. tz.add = addZone;
  474. tz.link = addLink;
  475. tz.load = loadData;
  476. tz.zone = getZone;
  477. tz.zoneExists = zoneExists; // deprecated in 0.1.0
  478. tz.guess = guess;
  479. tz.names = getNames;
  480. tz.Zone = Zone;
  481. tz.unpack = unpack;
  482. tz.unpackBase60 = unpackBase60;
  483. tz.needsOffset = needsOffset;
  484. tz.moveInvalidForward = true;
  485. tz.moveAmbiguousForward = false;
  486. tz.countries = getCountryNames;
  487. tz.zonesForCountry = zonesForCountry;
  488. /************************************
  489. Interface with Moment.js
  490. ************************************/
  491. var fn = moment.fn;
  492. moment.tz = tz;
  493. moment.defaultZone = null;
  494. moment.updateOffset = function (mom, keepTime) {
  495. var zone = moment.defaultZone,
  496. offset;
  497. if (mom._z === undefined) {
  498. if (zone && needsOffset(mom) && !mom._isUTC) {
  499. mom._d = moment.utc(mom._a)._d;
  500. mom.utc().add(zone.parse(mom), 'minutes');
  501. }
  502. mom._z = zone;
  503. }
  504. if (mom._z) {
  505. offset = mom._z.utcOffset(mom);
  506. if (Math.abs(offset) < 16) {
  507. offset = offset / 60;
  508. }
  509. if (mom.utcOffset !== undefined) {
  510. var z = mom._z;
  511. mom.utcOffset(-offset, keepTime);
  512. mom._z = z;
  513. } else {
  514. mom.zone(offset, keepTime);
  515. }
  516. }
  517. };
  518. fn.tz = function (name, keepTime) {
  519. if (name) {
  520. if (typeof name !== 'string') {
  521. throw new Error('Time zone name must be a string, got ' + name + ' [' + typeof name + ']');
  522. }
  523. this._z = getZone(name);
  524. if (this._z) {
  525. moment.updateOffset(this, keepTime);
  526. } else {
  527. logError("Moment Timezone has no data for " + name + ". See http://momentjs.com/timezone/docs/#/data-loading/.");
  528. }
  529. return this;
  530. }
  531. if (this._z) { return this._z.name; }
  532. };
  533. function abbrWrap (old) {
  534. return function () {
  535. if (this._z) { return this._z.abbr(this); }
  536. return old.call(this);
  537. };
  538. }
  539. function resetZoneWrap (old) {
  540. return function () {
  541. this._z = null;
  542. return old.apply(this, arguments);
  543. };
  544. }
  545. function resetZoneWrap2 (old) {
  546. return function () {
  547. if (arguments.length > 0) this._z = null;
  548. return old.apply(this, arguments);
  549. };
  550. }
  551. fn.zoneName = abbrWrap(fn.zoneName);
  552. fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
  553. fn.utc = resetZoneWrap(fn.utc);
  554. fn.local = resetZoneWrap(fn.local);
  555. fn.utcOffset = resetZoneWrap2(fn.utcOffset);
  556. moment.tz.setDefault = function(name) {
  557. if (major < 2 || (major === 2 && minor < 9)) {
  558. logError('Moment Timezone setDefault() requires Moment.js >= 2.9.0. You are using Moment.js ' + moment.version + '.');
  559. }
  560. moment.defaultZone = name ? getZone(name) : null;
  561. return moment;
  562. };
  563. // Cloning a moment should include the _z property.
  564. var momentProperties = moment.momentProperties;
  565. if (Object.prototype.toString.call(momentProperties) === '[object Array]') {
  566. // moment 2.8.1+
  567. momentProperties.push('_z');
  568. momentProperties.push('_a');
  569. } else if (momentProperties) {
  570. // moment 2.7.0
  571. momentProperties._z = null;
  572. }
  573. // INJECT DATA
  574. return moment;
  575. }));