Skip to content

Commit

Permalink
Merge pull request #322 from LeeHoward1/guessfix
Browse files Browse the repository at this point in the history
Checking for undefined Intl.DateTimeFormat().resolvedOptions().timeZone
  • Loading branch information
timrwood committed May 3, 2016
2 parents f3235dd + 2b2d803 commit 7732f4a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
10 changes: 6 additions & 4 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,13 @@
// use Intl API when available and returning valid time zone
try {
var intlName = Intl.DateTimeFormat().resolvedOptions().timeZone;
var name = names[normalizeName(intlName)];
if (name) {
return name;
if (intlName){
var name = names[normalizeName(intlName)];
if (name) {
return name;
}
logError("Moment Timezone found " + intlName + " from the Intl api, but did not have that data loaded.");
}
logError("Moment Timezone found " + intlName + " from the Intl api, but did not have that data loaded.");
} catch (e) {
// Intl unavailable, fall back to manual guessing.
}
Expand Down
16 changes: 16 additions & 0 deletions tests/moment-timezone/guess.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ exports.guess = {
test.done();
},

"When Intl is available, but timeZone is undefined, should return a guess without logging an error" : function (test) {
var oldError = console.error;
var errors = '';
console.error = function (message) {
errors += message;
};

mockIntlTimeZone(undefined);
mockTimezoneOffset(tz.zone('Europe/London'));
test.equal(tz.guess(true), 'Europe/London');
test.equal(errors, '');

console.error = oldError;
test.done();
},

"ensure each zone is represented" : function (test) {
var names = tz.names();
var zone, i;
Expand Down

0 comments on commit 7732f4a

Please sign in to comment.