moment-timezone-utils.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. //! moment-timezone-utils.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('./')); // 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 validatePackData (source) {
  109. if (!source.name) { throw new Error("Missing name"); }
  110. if (!source.abbrs) { throw new Error("Missing abbrs"); }
  111. if (!source.untils) { throw new Error("Missing untils"); }
  112. if (!source.offsets) { throw new Error("Missing offsets"); }
  113. if (
  114. source.offsets.length !== source.untils.length ||
  115. source.offsets.length !== source.abbrs.length
  116. ) {
  117. throw new Error("Mismatched array lengths");
  118. }
  119. }
  120. function pack (source) {
  121. validatePackData(source);
  122. return [
  123. source.name,
  124. packAbbrsAndOffsets(source),
  125. packUntils(source.untils) + packPopulation(source.population)
  126. ].join('|');
  127. }
  128. /************************************
  129. Create Links
  130. ************************************/
  131. function arraysAreEqual(a, b) {
  132. var i;
  133. if (a.length !== b.length) { return false; }
  134. for (i = 0; i < a.length; i++) {
  135. if (a[i] !== b[i]) {
  136. return false;
  137. }
  138. }
  139. return true;
  140. }
  141. function zonesAreEqual(a, b) {
  142. return arraysAreEqual(a.offsets, b.offsets) && arraysAreEqual(a.abbrs, b.abbrs) && arraysAreEqual(a.untils, b.untils);
  143. }
  144. function findAndCreateLinks (input, output, links, groupLeaders) {
  145. var i, j, a, b, group, foundGroup, groups = [];
  146. for (i = 0; i < input.length; i++) {
  147. foundGroup = false;
  148. a = input[i];
  149. for (j = 0; j < groups.length; j++) {
  150. group = groups[j];
  151. b = group[0];
  152. if (zonesAreEqual(a, b)) {
  153. if (a.population > b.population) {
  154. group.unshift(a);
  155. } else if (a.population === b.population && groupLeaders && groupLeaders[a.name]) {
  156. group.unshift(a);
  157. } else {
  158. group.push(a);
  159. }
  160. foundGroup = true;
  161. }
  162. }
  163. if (!foundGroup) {
  164. groups.push([a]);
  165. }
  166. }
  167. for (i = 0; i < groups.length; i++) {
  168. group = groups[i];
  169. output.push(group[0]);
  170. for (j = 1; j < group.length; j++) {
  171. links.push(group[0].name + '|' + group[j].name);
  172. }
  173. }
  174. }
  175. function createLinks (source, groupLeaders) {
  176. var zones = [],
  177. links = [];
  178. if (source.links) {
  179. links = source.links.slice();
  180. }
  181. findAndCreateLinks(source.zones, zones, links, groupLeaders);
  182. return {
  183. version : source.version,
  184. zones : zones,
  185. links : links.sort()
  186. };
  187. }
  188. /************************************
  189. Filter Years
  190. ************************************/
  191. function findStartAndEndIndex (untils, start, end) {
  192. var startI = 0,
  193. endI = untils.length + 1,
  194. untilYear,
  195. i;
  196. if (!end) {
  197. end = start;
  198. }
  199. if (start > end) {
  200. i = start;
  201. start = end;
  202. end = i;
  203. }
  204. for (i = 0; i < untils.length; i++) {
  205. if (untils[i] == null) {
  206. continue;
  207. }
  208. untilYear = new Date(untils[i]).getUTCFullYear();
  209. if (untilYear < start) {
  210. startI = i + 1;
  211. }
  212. if (untilYear > end) {
  213. endI = Math.min(endI, i + 1);
  214. }
  215. }
  216. return [startI, endI];
  217. }
  218. function filterYears (source, start, end) {
  219. var slice = Array.prototype.slice,
  220. indices = findStartAndEndIndex(source.untils, start, end),
  221. untils = slice.apply(source.untils, indices);
  222. untils[untils.length - 1] = null;
  223. return {
  224. name : source.name,
  225. abbrs : slice.apply(source.abbrs, indices),
  226. untils : untils,
  227. offsets : slice.apply(source.offsets, indices),
  228. population : source.population
  229. };
  230. }
  231. /************************************
  232. Filter, Link, and Pack
  233. ************************************/
  234. function filterLinkPack (input, start, end, groupLeaders) {
  235. var i,
  236. inputZones = input.zones,
  237. outputZones = [],
  238. output;
  239. for (i = 0; i < inputZones.length; i++) {
  240. outputZones[i] = filterYears(inputZones[i], start, end);
  241. }
  242. output = createLinks({
  243. zones : outputZones,
  244. links : input.links.slice(),
  245. version : input.version
  246. }, groupLeaders);
  247. for (i = 0; i < output.zones.length; i++) {
  248. output.zones[i] = pack(output.zones[i]);
  249. }
  250. return output;
  251. }
  252. /************************************
  253. Exports
  254. ************************************/
  255. moment.tz.pack = pack;
  256. moment.tz.packBase60 = packBase60;
  257. moment.tz.createLinks = createLinks;
  258. moment.tz.filterYears = filterYears;
  259. moment.tz.filterLinkPack = filterLinkPack;
  260. return moment;
  261. }));