moment-timezone-utils.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. //! moment-timezone-utils.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('./')); // 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. if (!moment.tz) {
  19. throw new Error("moment-timezone-utils.js must be loaded after moment-timezone.js");
  20. }
  21. /************************************
  22. Pack Base 60
  23. ************************************/
  24. var BASE60 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX',
  25. EPSILON = 0.000001; // Used to fix floating point rounding errors
  26. function packBase60Fraction(fraction, precision) {
  27. var buffer = '.',
  28. output = '',
  29. current;
  30. while (precision > 0) {
  31. precision -= 1;
  32. fraction *= 60;
  33. current = Math.floor(fraction + EPSILON);
  34. buffer += BASE60[current];
  35. fraction -= current;
  36. // Only add buffer to output once we have a non-zero value.
  37. // This makes '.000' output '', and '.100' output '.1'
  38. if (current) {
  39. output += buffer;
  40. buffer = '';
  41. }
  42. }
  43. return output;
  44. }
  45. function packBase60(number, precision) {
  46. var output = '',
  47. absolute = Math.abs(number),
  48. whole = Math.floor(absolute),
  49. fraction = packBase60Fraction(absolute - whole, Math.min(~~precision, 10));
  50. while (whole > 0) {
  51. output = BASE60[whole % 60] + output;
  52. whole = Math.floor(whole / 60);
  53. }
  54. if (number < 0) {
  55. output = '-' + output;
  56. }
  57. if (output && fraction) {
  58. return output + fraction;
  59. }
  60. if (!fraction && output === '-') {
  61. return '0';
  62. }
  63. return output || fraction || '0';
  64. }
  65. /************************************
  66. Pack
  67. ************************************/
  68. function packUntils(untils) {
  69. var out = [],
  70. last = 0,
  71. i;
  72. for (i = 0; i < untils.length - 1; i++) {
  73. out[i] = packBase60(Math.round((untils[i] - last) / 1000) / 60, 1);
  74. last = untils[i];
  75. }
  76. return out.join(' ');
  77. }
  78. function packAbbrsAndOffsets(source) {
  79. var index = 0,
  80. abbrs = [],
  81. offsets = [],
  82. indices = [],
  83. map = {},
  84. i, key;
  85. for (i = 0; i < source.abbrs.length; i++) {
  86. key = source.abbrs[i] + '|' + source.offsets[i];
  87. if (map[key] === undefined) {
  88. map[key] = index;
  89. abbrs[index] = source.abbrs[i];
  90. offsets[index] = packBase60(Math.round(source.offsets[i] * 60) / 60, 1);
  91. index++;
  92. }
  93. indices[i] = packBase60(map[key], 0);
  94. }
  95. return abbrs.join(' ') + '|' + offsets.join(' ') + '|' + indices.join('');
  96. }
  97. function packPopulation (number) {
  98. if (!number) {
  99. return '';
  100. }
  101. if (number < 1000) {
  102. return number;
  103. }
  104. var exponent = String(number | 0).length - 2;
  105. var precision = Math.round(number / Math.pow(10, exponent));
  106. return precision + 'e' + exponent;
  107. }
  108. function packCountries (countries) {
  109. if (!countries) {
  110. return '';
  111. }
  112. return countries.join(' ');
  113. }
  114. function validatePackData (source) {
  115. if (!source.name) { throw new Error("Missing name"); }
  116. if (!source.abbrs) { throw new Error("Missing abbrs"); }
  117. if (!source.untils) { throw new Error("Missing untils"); }
  118. if (!source.offsets) { throw new Error("Missing offsets"); }
  119. if (
  120. source.offsets.length !== source.untils.length ||
  121. source.offsets.length !== source.abbrs.length
  122. ) {
  123. throw new Error("Mismatched array lengths");
  124. }
  125. }
  126. function pack (source) {
  127. validatePackData(source);
  128. return [
  129. source.name, // 0 - timezone name
  130. packAbbrsAndOffsets(source), // 1 - abbrs, 2 - offsets, 3 - indices
  131. packUntils(source.untils), // 4 - untils
  132. packPopulation(source.population) // 5 - population
  133. ].join('|');
  134. }
  135. function packCountry (source) {
  136. return [
  137. source.name,
  138. source.zones.join(' '),
  139. ].join('|');
  140. }
  141. /************************************
  142. Create Links
  143. ************************************/
  144. function arraysAreEqual(a, b) {
  145. var i;
  146. if (a.length !== b.length) { return false; }
  147. for (i = 0; i < a.length; i++) {
  148. if (a[i] !== b[i]) {
  149. return false;
  150. }
  151. }
  152. return true;
  153. }
  154. function zonesAreEqual(a, b) {
  155. return arraysAreEqual(a.offsets, b.offsets) && arraysAreEqual(a.abbrs, b.abbrs) && arraysAreEqual(a.untils, b.untils);
  156. }
  157. function findAndCreateLinks (input, output, links, groupLeaders) {
  158. var i, j, a, b, group, foundGroup, groups = [];
  159. for (i = 0; i < input.length; i++) {
  160. foundGroup = false;
  161. a = input[i];
  162. for (j = 0; j < groups.length; j++) {
  163. group = groups[j];
  164. b = group[0];
  165. if (zonesAreEqual(a, b)) {
  166. if (a.population > b.population) {
  167. group.unshift(a);
  168. } else if (a.population === b.population && groupLeaders && groupLeaders[a.name]) {
  169. group.unshift(a);
  170. } else {
  171. group.push(a);
  172. }
  173. foundGroup = true;
  174. }
  175. }
  176. if (!foundGroup) {
  177. groups.push([a]);
  178. }
  179. }
  180. for (i = 0; i < groups.length; i++) {
  181. group = groups[i];
  182. output.push(group[0]);
  183. for (j = 1; j < group.length; j++) {
  184. links.push(group[0].name + '|' + group[j].name);
  185. }
  186. }
  187. }
  188. function createLinks (source, groupLeaders) {
  189. var zones = [],
  190. links = [];
  191. if (source.links) {
  192. links = source.links.slice();
  193. }
  194. findAndCreateLinks(source.zones, zones, links, groupLeaders);
  195. return {
  196. version : source.version,
  197. zones : zones,
  198. links : links.sort()
  199. };
  200. }
  201. /************************************
  202. Filter Years
  203. ************************************/
  204. function findStartAndEndIndex (untils, start, end) {
  205. var startI = 0,
  206. endI = untils.length + 1,
  207. untilYear,
  208. i;
  209. if (!end) {
  210. end = start;
  211. }
  212. if (start > end) {
  213. i = start;
  214. start = end;
  215. end = i;
  216. }
  217. for (i = 0; i < untils.length; i++) {
  218. if (untils[i] == null) {
  219. continue;
  220. }
  221. untilYear = new Date(untils[i]).getUTCFullYear();
  222. if (untilYear < start) {
  223. startI = i + 1;
  224. }
  225. if (untilYear > end) {
  226. endI = Math.min(endI, i + 1);
  227. }
  228. }
  229. return [startI, endI];
  230. }
  231. function filterYears (source, start, end) {
  232. var slice = Array.prototype.slice,
  233. indices = findStartAndEndIndex(source.untils, start, end),
  234. untils = slice.apply(source.untils, indices);
  235. untils[untils.length - 1] = null;
  236. return {
  237. name : source.name,
  238. abbrs : slice.apply(source.abbrs, indices),
  239. untils : untils,
  240. offsets : slice.apply(source.offsets, indices),
  241. population : source.population,
  242. countries : source.countries
  243. };
  244. }
  245. /************************************
  246. Filter, Link, and Pack
  247. ************************************/
  248. function filterLinkPack (input, start, end, groupLeaders) {
  249. var i,
  250. inputZones = input.zones,
  251. outputZones = [],
  252. output;
  253. for (i = 0; i < inputZones.length; i++) {
  254. outputZones[i] = filterYears(inputZones[i], start, end);
  255. }
  256. output = createLinks({
  257. zones : outputZones,
  258. links : input.links.slice(),
  259. version : input.version
  260. }, groupLeaders);
  261. for (i = 0; i < output.zones.length; i++) {
  262. output.zones[i] = pack(output.zones[i]);
  263. }
  264. output.countries = input.countries ? input.countries.map(function (unpacked) {
  265. return packCountry(unpacked);
  266. }) : [];
  267. return output;
  268. }
  269. /************************************
  270. Exports
  271. ************************************/
  272. moment.tz.pack = pack;
  273. moment.tz.packBase60 = packBase60;
  274. moment.tz.createLinks = createLinks;
  275. moment.tz.filterYears = filterYears;
  276. moment.tz.filterLinkPack = filterLinkPack;
  277. moment.tz.packCountry = packCountry;
  278. return moment;
  279. }));