Skip to content

Instantly share code, notes, and snippets.

@birjj
Created March 30, 2015 17:33
Show Gist options
  • Select an option

  • Save birjj/d5ecaa76674982019a6e to your computer and use it in GitHub Desktop.

Select an option

Save birjj/d5ecaa76674982019a6e to your computer and use it in GitHub Desktop.
(Much) faster tzdetect.js
var tzdetect = {
tzs: moment.tz.names(),
matches: function(){
return this.offsetToTimezone[(new Date()).getTimezoneOffset()];
}
};
// compute (new Date()).getTimezoneOffset value for all timezones, store in tzdetect.offsetToTimezone
(function(tzdetect){
var outp = {},
tzs = tzdetect.tzs;
for (var i = 0; i < tzs.length; ++i) {
var m = moment().tz(tzs[i]),
offset = m._offset;
if (!outp.hasOwnProperty(offset)) outp[offset] = [];
outp[offset].push(tzs[i]);
}
tzdetect.offsetToTimezone = outp;
})(tzdetect);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment