moment-timezone.js 15 KB

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