forked from moment/moment-timezone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
moment-timezone-utils.js
339 lines (279 loc) · 7.6 KB
/
moment-timezone-utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
//! moment-timezone-utils.js
//! version : 0.5.31
//! Copyright (c) JS Foundation and other contributors
//! license : MIT
//! github.com/moment/moment-timezone
(function (root, factory) {
"use strict";
/*global define*/
if (typeof module === 'object' && module.exports) {
module.exports = factory(require('./')); // Node
} else if (typeof define === 'function' && define.amd) {
define(['moment'], factory); // AMD
} else {
factory(root.moment); // Browser
}
}(this, function (moment) {
"use strict";
if (!moment.tz) {
throw new Error("moment-timezone-utils.js must be loaded after moment-timezone.js");
}
/************************************
Pack Base 60
************************************/
var BASE60 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX',
EPSILON = 0.000001; // Used to fix floating point rounding errors
function packBase60Fraction(fraction, precision) {
var buffer = '.',
output = '',
current;
while (precision > 0) {
precision -= 1;
fraction *= 60;
current = Math.floor(fraction + EPSILON);
buffer += BASE60[current];
fraction -= current;
// Only add buffer to output once we have a non-zero value.
// This makes '.000' output '', and '.100' output '.1'
if (current) {
output += buffer;
buffer = '';
}
}
return output;
}
function packBase60(number, precision) {
var output = '',
absolute = Math.abs(number),
whole = Math.floor(absolute),
fraction = packBase60Fraction(absolute - whole, Math.min(~~precision, 10));
while (whole > 0) {
output = BASE60[whole % 60] + output;
whole = Math.floor(whole / 60);
}
if (number < 0) {
output = '-' + output;
}
if (output && fraction) {
return output + fraction;
}
if (!fraction && output === '-') {
return '0';
}
return output || fraction || '0';
}
/************************************
Pack
************************************/
function packUntils(untils) {
var out = [],
last = 0,
i;
for (i = 0; i < untils.length - 1; i++) {
out[i] = packBase60(Math.round((untils[i] - last) / 1000) / 60, 1);
last = untils[i];
}
return out.join(' ');
}
function packAbbrsAndOffsets(source) {
var index = 0,
abbrs = [],
offsets = [],
indices = [],
map = {},
i, key;
for (i = 0; i < source.abbrs.length; i++) {
key = source.abbrs[i] + '|' + source.offsets[i];
if (map[key] === undefined) {
map[key] = index;
abbrs[index] = source.abbrs[i];
offsets[index] = packBase60(Math.round(source.offsets[i] * 60) / 60, 1);
index++;
}
indices[i] = packBase60(map[key], 0);
}
return abbrs.join(' ') + '|' + offsets.join(' ') + '|' + indices.join('');
}
function packPopulation (number) {
if (!number) {
return '';
}
if (number < 1000) {
return number;
}
var exponent = String(number | 0).length - 2;
var precision = Math.round(number / Math.pow(10, exponent));
return precision + 'e' + exponent;
}
function packCountries (countries) {
if (!countries) {
return '';
}
return countries.join(' ');
}
function validatePackData (source) {
if (!source.name) { throw new Error("Missing name"); }
if (!source.abbrs) { throw new Error("Missing abbrs"); }
if (!source.untils) { throw new Error("Missing untils"); }
if (!source.offsets) { throw new Error("Missing offsets"); }
if (
source.offsets.length !== source.untils.length ||
source.offsets.length !== source.abbrs.length
) {
throw new Error("Mismatched array lengths");
}
}
function pack (source) {
validatePackData(source);
return [
source.name, // 0 - timezone name
packAbbrsAndOffsets(source), // 1 - abbrs, 2 - offsets, 3 - indices
packUntils(source.untils), // 4 - untils
packPopulation(source.population) // 5 - population
].join('|');
}
function packCountry (source) {
return [
source.name,
source.zones.join(' '),
].join('|');
}
/************************************
Create Links
************************************/
function arraysAreEqual(a, b) {
var i;
if (a.length !== b.length) { return false; }
for (i = 0; i < a.length; i++) {
if (a[i] !== b[i]) {
return false;
}
}
return true;
}
function zonesAreEqual(a, b) {
return arraysAreEqual(a.offsets, b.offsets) && arraysAreEqual(a.abbrs, b.abbrs) && arraysAreEqual(a.untils, b.untils);
}
function findAndCreateLinks (input, output, links, groupLeaders) {
var i, j, a, b, group, foundGroup, groups = [];
for (i = 0; i < input.length; i++) {
foundGroup = false;
a = input[i];
for (j = 0; j < groups.length; j++) {
group = groups[j];
b = group[0];
if (zonesAreEqual(a, b)) {
if (a.population > b.population) {
group.unshift(a);
} else if (a.population === b.population && groupLeaders && groupLeaders[a.name]) {
group.unshift(a);
} else {
group.push(a);
}
foundGroup = true;
}
}
if (!foundGroup) {
groups.push([a]);
}
}
for (i = 0; i < groups.length; i++) {
group = groups[i];
output.push(group[0]);
for (j = 1; j < group.length; j++) {
links.push(group[0].name + '|' + group[j].name);
}
}
}
function createLinks (source, groupLeaders) {
var zones = [],
links = [];
if (source.links) {
links = source.links.slice();
}
findAndCreateLinks(source.zones, zones, links, groupLeaders);
return {
version : source.version,
zones : zones,
links : links.sort()
};
}
/************************************
Filter Years
************************************/
function findStartAndEndIndex (untils, start, end) {
var startI = 0,
endI = untils.length + 1,
untilYear,
i;
if (!end) {
end = start;
}
if (start > end) {
i = start;
start = end;
end = i;
}
for (i = 0; i < untils.length; i++) {
if (untils[i] == null) {
continue;
}
untilYear = new Date(untils[i]).getUTCFullYear();
if (untilYear < start) {
startI = i + 1;
}
if (untilYear > end) {
endI = Math.min(endI, i + 1);
}
}
return [startI, endI];
}
function filterYears (source, start, end) {
var slice = Array.prototype.slice,
indices = findStartAndEndIndex(source.untils, start, end),
untils = slice.apply(source.untils, indices);
untils[untils.length - 1] = null;
return {
name : source.name,
abbrs : slice.apply(source.abbrs, indices),
untils : untils,
offsets : slice.apply(source.offsets, indices),
population : source.population,
countries : source.countries
};
}
/************************************
Filter, Link, and Pack
************************************/
function filterLinkPack (input, start, end, groupLeaders) {
var i,
inputZones = input.zones,
outputZones = [],
output;
for (i = 0; i < inputZones.length; i++) {
outputZones[i] = filterYears(inputZones[i], start, end);
}
output = createLinks({
zones : outputZones,
links : input.links.slice(),
version : input.version
}, groupLeaders);
for (i = 0; i < output.zones.length; i++) {
output.zones[i] = pack(output.zones[i]);
}
output.countries = input.countries ? input.countries.map(function (unpacked) {
return packCountry(unpacked);
}) : [];
return output;
}
/************************************
Exports
************************************/
moment.tz.pack = pack;
moment.tz.packBase60 = packBase60;
moment.tz.createLinks = createLinks;
moment.tz.filterYears = filterYears;
moment.tz.filterLinkPack = filterLinkPack;
moment.tz.packCountry = packCountry;
return moment;
}));