Skip to content

Instantly share code, notes, and snippets.

@StanAngeloff
Created October 10, 2012 14:52
Show Gist options
  • Save StanAngeloff/3866114 to your computer and use it in GitHub Desktop.
Save StanAngeloff/3866114 to your computer and use it in GitHub Desktop.
// Taken from https://bitbucket.org/pellepim/jstimezonedetect/raw/default/detect_timezone.js
/*version 2012-05-10*/
/**
* Namespace to hold all the code for timezone detection.
*/
var jstz = (function () {
'use strict';
var HEMISPHERE_SOUTH = 'S',
HEMISPHERE_NORTH = 'N';
/**
* Gets the offset in minutes from UTC for a certain date.
* @param {Date} date
* @returns {Number}
*/
function getDateOffset(date) {
var offset = -date.getTimezoneOffset();
return (offset !== null ? offset : 0);
}
function getJanuaryOffset() {
return getDateOffset(new Date(2010, 0, 1, 0, 0, 0, 0));
}
function getJuneOffset() {
return getDateOffset(new Date(2010, 5, 1, 0, 0, 0, 0));
}
/**
* This function does some basic calculations to create information about
* the user's timezone.
*
* Returns a key that can be used to do lookups in jstz.olson.timezones.
*
* @returns {String}
*/
function lookup() {
var januaryOffset = getJanuaryOffset(),
juneOffset = getJuneOffset(),
diff = (getJanuaryOffset() - getJuneOffset());
if (diff < 0) {
return [januaryOffset, true, HEMISPHERE_NORTH];
} else if (diff > 0) {
return [juneOffset, true, HEMISPHERE_SOUTH];
}
return [januaryOffset, false, HEMISPHERE_NORTH];
}
/*
* The values in the returned list are as such:
*
* First the offset compared to UTC time in minutes.
*
* Then a flag which is FALSE if the timezone does not take daylight savings
* into account and TRUE if it does.
*
* Thirdly an optional {S,N} signifies that the timezone is in the northen
* or southers hemisphere, only interesting for timezones with DST.
*/
return lookup;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment