Skip to content

Commit aea92e2

Browse files
committed
Bugfix: zone country is detected incorrect if link is used
1 parent dbf9f2e commit aea92e2

File tree

5 files changed

+155
-245
lines changed

5 files changed

+155
-245
lines changed

builds/moment-timezone-with-data-10-year-range.js

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,20 @@
118118
var data = string.split('|'),
119119
offsets = data[2].split(' '),
120120
indices = data[3].split(''),
121-
untils = data[4].split(' '),
122-
countries = data[6];
121+
untils = data[4].split(' ');
123122

124123
arrayToInt(offsets);
125124
arrayToInt(indices);
126125
arrayToInt(untils);
127126

128127
intToUntil(untils, indices.length);
129128

130-
if (countries) {
131-
countries = countries.split(' ');
132-
} else {
133-
countries = [];
134-
}
135-
136129
return {
137130
name : data[0],
138131
abbrs : mapIndices(data[1].split(' '), indices),
139132
offsets : mapIndices(offsets, indices),
140133
untils : untils,
141-
population : data[5] | 0,
142-
countries : countries
143-
};
144-
}
145-
146-
function unpackCountry (string) {
147-
var data = string.split('|'),
148-
zones = data[1].split(' ');
149-
150-
return {
151-
name : data[0],
152-
zones : zones
134+
population : data[5] | 0
153135
};
154136
}
155137

@@ -170,7 +152,7 @@
170152
this.untils = unpacked.untils;
171153
this.offsets = unpacked.offsets;
172154
this.population = unpacked.population;
173-
this.countries = unpacked.countries;
155+
this.countries = this._getCountries(this.name);
174156
},
175157

176158
_index : function (timestamp) {
@@ -185,6 +167,13 @@
185167
}
186168
},
187169

170+
_getCountries : function () {
171+
var zone_name = this.name;
172+
return Object.keys(countries).filter(function (country_code) {
173+
return countries[country_code].zones.indexOf(zone_name) !== -1;
174+
});
175+
},
176+
188177
parse : function (timestamp) {
189178
var target = +timestamp,
190179
offsets = this.offsets,
@@ -229,20 +218,11 @@
229218
Country object
230219
************************************/
231220

232-
function Country (packedString) {
233-
if (packedString) {
234-
this._set(unpackCountry(packedString));
235-
}
221+
function Country (country_name, zone_names) {
222+
this.name = country_name;
223+
this.zones = zone_names;
236224
}
237225

238-
Country.prototype = {
239-
_set : function (unpacked) {
240-
this.name = unpacked.name;
241-
this.zones = unpacked.zones;
242-
}
243-
};
244-
245-
246226
/************************************
247227
Current Timezone
248228
************************************/
@@ -459,6 +439,7 @@
459439
zone = zones[name] = new Zone();
460440
zone._set(link);
461441
zone.name = names[name];
442+
zone.countries = zone._getCountries()
462443
return zone;
463444
}
464445

@@ -499,7 +480,7 @@
499480
}
500481

501482
function addCountries (data) {
502-
var i, name, split, normalized;
483+
var i, country_code, country_zones, split, normalized_country_code;
503484
//check required because data could be undefined.
504485
if (data) {
505486
if (typeof data === "string") {
@@ -508,26 +489,21 @@
508489

509490
for (i = 0; i < data.length; i++) {
510491
split = data[i].split('|');
511-
name = split[0];
512-
normalized = normalizeName(name);
513-
countries[normalized] = data[i];
492+
country_code = split[0].toUpperCase();
493+
country_zones = split[1].split(' ');
494+
countries[country_code] = new Country(
495+
country_code,
496+
country_zones
497+
);
514498
}
515499
}
516500
}
517501

518502
function getCountry (name) {
519-
name = normalizeName(name);
520-
521-
var country = countries[name];
503+
name = name.toUpperCase();
522504

523-
if (country instanceof Country) {
524-
return country;
525-
}
526-
527-
if (typeof country === 'string') {
528-
country = new Country(country);
529-
countries[name] = country;
530-
return country;
505+
if (countries[name] instanceof Country) {
506+
return countries[name];
531507
}
532508

533509
return null;
@@ -537,7 +513,13 @@
537513
country = getCountry(country);
538514

539515
if (country) {
540-
return country.zones;
516+
return country.zones.map(function (zone_name) {
517+
var zone = getZone(zone_name);
518+
return {
519+
name: zone_name,
520+
offset: zone.offset(new Date())
521+
}
522+
});
541523
}
542524

543525
return null;

builds/moment-timezone-with-data-1970-2030.js

Lines changed: 31 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -118,38 +118,20 @@
118118
var data = string.split('|'),
119119
offsets = data[2].split(' '),
120120
indices = data[3].split(''),
121-
untils = data[4].split(' '),
122-
countries = data[6];
121+
untils = data[4].split(' ');
123122

124123
arrayToInt(offsets);
125124
arrayToInt(indices);
126125
arrayToInt(untils);
127126

128127
intToUntil(untils, indices.length);
129128

130-
if (countries) {
131-
countries = countries.split(' ');
132-
} else {
133-
countries = [];
134-
}
135-
136129
return {
137130
name : data[0],
138131
abbrs : mapIndices(data[1].split(' '), indices),
139132
offsets : mapIndices(offsets, indices),
140133
untils : untils,
141-
population : data[5] | 0,
142-
countries : countries
143-
};
144-
}
145-
146-
function unpackCountry (string) {
147-
var data = string.split('|'),
148-
zones = data[1].split(' ');
149-
150-
return {
151-
name : data[0],
152-
zones : zones
134+
population : data[5] | 0
153135
};
154136
}
155137

@@ -170,7 +152,7 @@
170152
this.untils = unpacked.untils;
171153
this.offsets = unpacked.offsets;
172154
this.population = unpacked.population;
173-
this.countries = unpacked.countries;
155+
this.countries = this._getCountries(this.name);
174156
},
175157

176158
_index : function (timestamp) {
@@ -185,6 +167,13 @@
185167
}
186168
},
187169

170+
_getCountries : function () {
171+
var zone_name = this.name;
172+
return Object.keys(countries).filter(function (country_code) {
173+
return countries[country_code].zones.indexOf(zone_name) !== -1;
174+
});
175+
},
176+
188177
parse : function (timestamp) {
189178
var target = +timestamp,
190179
offsets = this.offsets,
@@ -229,20 +218,11 @@
229218
Country object
230219
************************************/
231220

232-
function Country (packedString) {
233-
if (packedString) {
234-
this._set(unpackCountry(packedString));
235-
}
221+
function Country (country_name, zone_names) {
222+
this.name = country_name;
223+
this.zones = zone_names;
236224
}
237225

238-
Country.prototype = {
239-
_set : function (unpacked) {
240-
this.name = unpacked.name;
241-
this.zones = unpacked.zones;
242-
}
243-
};
244-
245-
246226
/************************************
247227
Current Timezone
248228
************************************/
@@ -459,6 +439,7 @@
459439
zone = zones[name] = new Zone();
460440
zone._set(link);
461441
zone.name = names[name];
442+
zone.countries = zone._getCountries()
462443
return zone;
463444
}
464445

@@ -499,7 +480,7 @@
499480
}
500481

501482
function addCountries (data) {
502-
var i, name, split, normalized;
483+
var i, country_code, country_zones, split, normalized_country_code;
503484
//check required because data could be undefined.
504485
if (data) {
505486
if (typeof data === "string") {
@@ -508,26 +489,21 @@
508489

509490
for (i = 0; i < data.length; i++) {
510491
split = data[i].split('|');
511-
name = split[0];
512-
normalized = normalizeName(name);
513-
countries[normalized] = data[i];
492+
country_code = split[0].toUpperCase();
493+
country_zones = split[1].split(' ');
494+
countries[country_code] = new Country(
495+
country_code,
496+
country_zones
497+
);
514498
}
515499
}
516500
}
517501

518502
function getCountry (name) {
519-
name = normalizeName(name);
520-
521-
var country = countries[name];
503+
name = name.toUpperCase();
522504

523-
if (country instanceof Country) {
524-
return country;
525-
}
526-
527-
if (typeof country === 'string') {
528-
country = new Country(country);
529-
countries[name] = country;
530-
return country;
505+
if (countries[name] instanceof Country) {
506+
return countries[name];
531507
}
532508

533509
return null;
@@ -537,7 +513,13 @@
537513
country = getCountry(country);
538514

539515
if (country) {
540-
return country.zones;
516+
return country.zones.map(function (zone_name) {
517+
var zone = getZone(zone_name);
518+
return {
519+
name: zone_name,
520+
offset: zone.offset(new Date())
521+
}
522+
});
541523
}
542524

543525
return null;

0 commit comments

Comments
 (0)