Wiki

Clone wiki

jsTimezoneDetect / About_the_code

Basic algorithm

For the following example, imagine that your user has his computer's time set to Anchorage (Alaska).

The method below returns a little object containing information about the main offset (not DST offset), a flag for whether the zone uses daylight savings, and if applicable, whether the zone is in the northern our southern hemisphere.

function get_timezone_info() {
    // Alaska offset in january is -540 minutes from UTC
    var january_offset = get_january_offset(); 
	
    // Alaska offset in june is -480 minutes from UTC
    var june_offset = get_june_offset(); // returns 480
	
    // There's a -60 minutes offset difference between january and june in Alaska
    var diff = january_offset - june_offset; 
        
    // Diff = -60, so we know that this region uses daylight savings,
    // and is in the northern hemisphere. Also we know that the January
    // offset is the common denominator for the timezone, i.e 540 minutes = 9 hours
    if (diff < 0) {
        return {'utc_offset' : january_offset,
                'dst':	1,
                'hemisphere' : HEMISPHERE_NORTH}
    }

    // This is where we end up for southern hemisphere daylight savings time zones
    else if (diff > 0) {
        return {'utc_offset' : june_offset,
                'dst' : 1,
                'hemisphere' : HEMISPHERE_SOUTH}
    }

    // this is returned for non DST time zones
    return {'utc_offset' : january_offset, 
            'dst': 0, 
            'hemisphere' : HEMISPHERE_UNKNOWN}
}

Here are some return examples for different timezones:

// Central Australian Time (Audelaide): 
{utc_offset=570, dst=1, hemisphere="south"}

// Central Australian Standard Time (Darwin)
{utc_offset=570, dst=0, hemisphere="N/A"}

// Central European Time
{utc_offset=60, dst=true, hemisphere="north"}

// Alaska
{utc_offset=-540, dst=1, hemisphere="north"}

// Mountain Time
{utc_offset=-420, dst=1, hemisphere="north"}

The last of the examples above is actually ambiguous. There are two versions of Mountain Time which uses daylight savings. One in Mexico and one in US/Canada. The difference is when they start their daylight savings.

Fortunately we have some possibility of being even more granular with our determination by checking some specifically known dates.

Solving ambiguous cases

In the case of Mountain Time (UTC-7), there are actually three sub timezones.

  • Arizona uses UTC-7 and does not use daylight saving.
  • US Mountain Time (Denver etc) is UTC-7 and daylight savings starts 13 March 2011.
  • Mexico Mountain Time (Chihuahua etc) is UTC-7 and daylight savings starts 3 April 2011.

If the script above would return. {utc_offset=-7, dst=true, hemisphere="north"} we can only be sure that it is either US or Mexico Mountain time. But since we know some specific dates when daylight saving has started for these two regions, we can simply check them in order.

We start by checking 13 March 2011 and see check its offsets (with the algorithms above) if it is summer time. If so, we have a match for US Mountain Time. Otherwise we check 3 April 2011, and if that matches we have Mexico Mountain Time.

There are a few ambiguous cases out there and jsTimezoneDetect solves most of them for you. If there is only a few hours difference between the times when daylight saving starts for two timezones jsTimezoneDetect might not be able to be exact. This all depends on your user's OS and browser. Usually though, jsTimezoneDetect will give you are very close match.

Updated