moment-timezone.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. //! moment-timezone.js
  2. //! version : 0.5.27
  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.27",
  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. if (a.zone.population !== b.zone.population) {
  245. return b.zone.population - a.zone.population;
  246. }
  247. return b.zone.name.localeCompare(a.zone.name);
  248. }
  249. function addToGuesses (name, offsets) {
  250. var i, offset;
  251. arrayToInt(offsets);
  252. for (i = 0; i < offsets.length; i++) {
  253. offset = offsets[i];
  254. guesses[offset] = guesses[offset] || {};
  255. guesses[offset][name] = true;
  256. }
  257. }
  258. function guessesForUserOffsets (offsets) {
  259. var offsetsLength = offsets.length,
  260. filteredGuesses = {},
  261. out = [],
  262. i, j, guessesOffset;
  263. for (i = 0; i < offsetsLength; i++) {
  264. guessesOffset = guesses[offsets[i].offset] || {};
  265. for (j in guessesOffset) {
  266. if (guessesOffset.hasOwnProperty(j)) {
  267. filteredGuesses[j] = true;
  268. }
  269. }
  270. }
  271. for (i in filteredGuesses) {
  272. if (filteredGuesses.hasOwnProperty(i)) {
  273. out.push(names[i]);
  274. }
  275. }
  276. return out;
  277. }
  278. function rebuildGuess () {
  279. // use Intl API when available and returning valid time zone
  280. try {
  281. var intlName = Intl.DateTimeFormat().resolvedOptions().timeZone;
  282. if (intlName && intlName.length > 3) {
  283. var name = names[normalizeName(intlName)];
  284. if (name) {
  285. return name;
  286. }
  287. logError("Moment Timezone found " + intlName + " from the Intl api, but did not have that data loaded.");
  288. }
  289. } catch (e) {
  290. // Intl unavailable, fall back to manual guessing.
  291. }
  292. var offsets = userOffsets(),
  293. offsetsLength = offsets.length,
  294. guesses = guessesForUserOffsets(offsets),
  295. zoneScores = [],
  296. zoneScore, i, j;
  297. for (i = 0; i < guesses.length; i++) {
  298. zoneScore = new ZoneScore(getZone(guesses[i]), offsetsLength);
  299. for (j = 0; j < offsetsLength; j++) {
  300. zoneScore.scoreOffsetAt(offsets[j]);
  301. }
  302. zoneScores.push(zoneScore);
  303. }
  304. zoneScores.sort(sortZoneScores);
  305. return zoneScores.length > 0 ? zoneScores[0].zone.name : undefined;
  306. }
  307. function guess (ignoreCache) {
  308. if (!cachedGuess || ignoreCache) {
  309. cachedGuess = rebuildGuess();
  310. }
  311. return cachedGuess;
  312. }
  313. /************************************
  314. Global Methods
  315. ************************************/
  316. function normalizeName (name) {
  317. return (name || '').toLowerCase().replace(/\//g, '_');
  318. }
  319. function addZone (packed) {
  320. var i, name, split, normalized;
  321. if (typeof packed === "string") {
  322. packed = [packed];
  323. }
  324. for (i = 0; i < packed.length; i++) {
  325. split = packed[i].split('|');
  326. name = split[0];
  327. normalized = normalizeName(name);
  328. zones[normalized] = packed[i];
  329. names[normalized] = name;
  330. addToGuesses(normalized, split[2].split(' '));
  331. }
  332. }
  333. function getZone (name, caller) {
  334. name = normalizeName(name);
  335. var zone = zones[name];
  336. var link;
  337. if (zone instanceof Zone) {
  338. return zone;
  339. }
  340. if (typeof zone === 'string') {
  341. zone = new Zone(zone);
  342. zones[name] = zone;
  343. return zone;
  344. }
  345. // Pass getZone to prevent recursion more than 1 level deep
  346. if (links[name] && caller !== getZone && (link = getZone(links[name], getZone))) {
  347. zone = zones[name] = new Zone();
  348. zone._set(link);
  349. zone.name = names[name];
  350. return zone;
  351. }
  352. return null;
  353. }
  354. function getNames () {
  355. var i, out = [];
  356. for (i in names) {
  357. if (names.hasOwnProperty(i) && (zones[i] || zones[links[i]]) && names[i]) {
  358. out.push(names[i]);
  359. }
  360. }
  361. return out.sort();
  362. }
  363. function addLink (aliases) {
  364. var i, alias, normal0, normal1;
  365. if (typeof aliases === "string") {
  366. aliases = [aliases];
  367. }
  368. for (i = 0; i < aliases.length; i++) {
  369. alias = aliases[i].split('|');
  370. normal0 = normalizeName(alias[0]);
  371. normal1 = normalizeName(alias[1]);
  372. links[normal0] = normal1;
  373. names[normal0] = alias[0];
  374. links[normal1] = normal0;
  375. names[normal1] = alias[1];
  376. }
  377. }
  378. function loadData (data) {
  379. addZone(data.zones);
  380. addLink(data.links);
  381. tz.dataVersion = data.version;
  382. }
  383. function zoneExists (name) {
  384. if (!zoneExists.didShowError) {
  385. zoneExists.didShowError = true;
  386. logError("moment.tz.zoneExists('" + name + "') has been deprecated in favor of !moment.tz.zone('" + name + "')");
  387. }
  388. return !!getZone(name);
  389. }
  390. function needsOffset (m) {
  391. var isUnixTimestamp = (m._f === 'X' || m._f === 'x');
  392. return !!(m._a && (m._tzm === undefined) && !isUnixTimestamp);
  393. }
  394. function logError (message) {
  395. if (typeof console !== 'undefined' && typeof console.error === 'function') {
  396. console.error(message);
  397. }
  398. }
  399. /************************************
  400. moment.tz namespace
  401. ************************************/
  402. function tz (input) {
  403. var args = Array.prototype.slice.call(arguments, 0, -1),
  404. name = arguments[arguments.length - 1],
  405. zone = getZone(name),
  406. out = moment.utc.apply(null, args);
  407. if (zone && !moment.isMoment(input) && needsOffset(out)) {
  408. out.add(zone.parse(out), 'minutes');
  409. }
  410. out.tz(name);
  411. return out;
  412. }
  413. tz.version = VERSION;
  414. tz.dataVersion = '';
  415. tz._zones = zones;
  416. tz._links = links;
  417. tz._names = names;
  418. tz.add = addZone;
  419. tz.link = addLink;
  420. tz.load = loadData;
  421. tz.zone = getZone;
  422. tz.zoneExists = zoneExists; // deprecated in 0.1.0
  423. tz.guess = guess;
  424. tz.names = getNames;
  425. tz.Zone = Zone;
  426. tz.unpack = unpack;
  427. tz.unpackBase60 = unpackBase60;
  428. tz.needsOffset = needsOffset;
  429. tz.moveInvalidForward = true;
  430. tz.moveAmbiguousForward = false;
  431. /************************************
  432. Interface with Moment.js
  433. ************************************/
  434. var fn = moment.fn;
  435. moment.tz = tz;
  436. moment.defaultZone = null;
  437. moment.updateOffset = function (mom, keepTime) {
  438. var zone = moment.defaultZone,
  439. offset;
  440. if (mom._z === undefined) {
  441. if (zone && needsOffset(mom) && !mom._isUTC) {
  442. mom._d = moment.utc(mom._a)._d;
  443. mom.utc().add(zone.parse(mom), 'minutes');
  444. }
  445. mom._z = zone;
  446. }
  447. if (mom._z) {
  448. offset = mom._z.utcOffset(mom);
  449. if (Math.abs(offset) < 16) {
  450. offset = offset / 60;
  451. }
  452. if (mom.utcOffset !== undefined) {
  453. var z = mom._z;
  454. mom.utcOffset(-offset, keepTime);
  455. mom._z = z;
  456. } else {
  457. mom.zone(offset, keepTime);
  458. }
  459. }
  460. };
  461. fn.tz = function (name, keepTime) {
  462. if (name) {
  463. if (typeof name !== 'string') {
  464. throw new Error('Time zone name must be a string, got ' + name + ' [' + typeof name + ']');
  465. }
  466. this._z = getZone(name);
  467. if (this._z) {
  468. moment.updateOffset(this, keepTime);
  469. } else {
  470. logError("Moment Timezone has no data for " + name + ". See http://momentjs.com/timezone/docs/#/data-loading/.");
  471. }
  472. return this;
  473. }
  474. if (this._z) { return this._z.name; }
  475. };
  476. function abbrWrap (old) {
  477. return function () {
  478. if (this._z) { return this._z.abbr(this); }
  479. return old.call(this);
  480. };
  481. }
  482. function resetZoneWrap (old) {
  483. return function () {
  484. this._z = null;
  485. return old.apply(this, arguments);
  486. };
  487. }
  488. function resetZoneWrap2 (old) {
  489. return function () {
  490. if (arguments.length > 0) this._z = null;
  491. return old.apply(this, arguments);
  492. };
  493. }
  494. fn.zoneName = abbrWrap(fn.zoneName);
  495. fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
  496. fn.utc = resetZoneWrap(fn.utc);
  497. fn.local = resetZoneWrap(fn.local);
  498. fn.utcOffset = resetZoneWrap2(fn.utcOffset);
  499. moment.tz.setDefault = function(name) {
  500. if (major < 2 || (major === 2 && minor < 9)) {
  501. logError('Moment Timezone setDefault() requires Moment.js >= 2.9.0. You are using Moment.js ' + moment.version + '.');
  502. }
  503. moment.defaultZone = name ? getZone(name) : null;
  504. return moment;
  505. };
  506. // Cloning a moment should include the _z property.
  507. var momentProperties = moment.momentProperties;
  508. if (Object.prototype.toString.call(momentProperties) === '[object Array]') {
  509. // moment 2.8.1+
  510. momentProperties.push('_z');
  511. momentProperties.push('_a');
  512. } else if (momentProperties) {
  513. // moment 2.7.0
  514. momentProperties._z = null;
  515. }
  516. // INJECT DATA
  517. return moment;
  518. }));