Skip to content

Instantly share code, notes, and snippets.

@jeffreywescott
Last active August 28, 2015 19:44
Show Gist options
  • Save jeffreywescott/c9bd1ad4f7ff97778977 to your computer and use it in GitHub Desktop.
Save jeffreywescott/c9bd1ad4f7ff97778977 to your computer and use it in GitHub Desktop.
client-side module to get Keen.io-compatible timezone string
var keenTimezone = function() {
var
jan = new Date(now.getFullYear(), 0, 1),
jul = new Date(now.getFullYear(), 6, 1),
now = new Date(),
tzOffset = Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset()) * -1 / 60,
dstTzOffset= now.getTimezoneOffset() * -1 / 60,
offsetsAre = function(expTzOffset, expDstTzOffset) {
if (tzOffset === expTzOffset && dstTzOffset === expDstTzOffset) {
return true
}
return false
}
;
if (offsetsAre(-10, -10)) {
return 'US/Hawaii'
}
if (offsetsAre(-9, -8)) {
return 'US/Alaska'
}
if (offsetsAre(-8, -7)) {
return 'US/Pacific'
}
if (offsetsAre(-7, -6)) {
return 'US/Mountain'
}
if (offsetsAre(-6, -5)) {
return 'US/Central'
}
if (offsetsAre(-5, -4)) {
return 'US/Eastern'
}
if (offsetsAre(-3, -2)) {
return 'America/Sao_Paulo'
}
if (offsetsAre(0, 0)) {
return 'UTC'
}
if (offsetsAre(0, 1)) {
return 'Africa/Casablanca'
}
if (offsetsAre(0, 1)) {
return 'Europe/London'
}
if (offsetsAre(1, 2)) {
return 'Europe/Amsterdam'
}
if (offsetsAre(1, 2)) {
return 'Europe/Paris'
}
if (offsetsAre(1, 2)) {
return 'Europe/Prague'
}
if (offsetsAre(1, 2)) {
return 'Europe/Stockholm'
}
if (offsetsAre(1, 2)) {
return 'Europe/Copenhagen'
}
if (offsetsAre(2, 3)) {
return 'Asia/Istanbul'
}
if (offsetsAre(2, 3)) {
return 'Europe/Istanbul'
}
if (offsetsAre(3, 3)) {
return 'Africa/Nairobi'
}
if (offsetsAre(4, 4)) {
return 'Asia/Dubai'
}
if (offsetsAre(7, 7)) {
return 'Asia/Jakarta'
}
if (offsetsAre(8, 8)) {
return 'Asia/Singapore'
}
if (offsetsAre(9, 9)) {
return 'Asia/Tokyo'
}
if (offsetsAre(8, 8)) {
return 'Australia/Perth'
}
if (offsetsAre(10, 11)) {
return 'Australia/Sydney'
}
if (offsetsAre(12, 13)) {
return 'Pacific/Auckland'
}
throw new Error('Unsupported Timezone')
}
module.exports = keenTimezone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment