Commits

Jon Nylander committed dc15ba6

Starting 'Experimenting with IP lookups for more exactness' branch.

Will optional IP lookups add exactness to the script result? I'll spend some time investigating.

Comments (0)

Files changed (1)

detect_timezone.js

 var jstz = (function () {
     'use strict';
     var HEMISPHERE_SOUTH = 'SOUTH',
+        preliminary_result = null,
+        callback = null,
         
         /** 
          * Gets the offset in minutes from UTC for a certain date.
             return january_offset + ",0";
         },
     
+        doIpLookup = function (preliminary_result, callback) {
+            var url = "http://www.pageloom.com/ip-to-tz/?callback=jstz.handleIpLookup";
+            var script = document.createElement('script');
+            script.src = url;
+            document.body.appendChild(script);
+        },
+        
+        handleIpLookup = function (ip_timezone_data) {
+            if (ip_timezone_data.timezone !== preliminary_result.name()) {
+                preliminary_result.compare_with(ip_timezone_data);
+                callback(preliminary_result);
+                return;
+            }
+            callback(preliminary_result);
+        },
+        
         /**
          * Uses get_timezone_info() to formulate a key to use in the olson.timezones dictionary.
          * 
          * 
          * @returns Object 
          */
-        determine_timezone = function () {
+        determine_timezone = function (return_function) {
             var key = lookup_key();
-            return new jstz.TimeZone(jstz.olson.timezones[key]);
+            
+            preliminary_result = new jstz.TimeZone(jstz.olson.timezones[key]);
+            
+            if (!return_function) {
+                return preliminary_result;
+            }
+            
+            callback = return_function;
+            
+            doIpLookup(preliminary_result, callback);
         };
     
     return {
         determine_timezone : determine_timezone,
+        handleIpLookup : handleIpLookup,
         date_is_dst : date_is_dst
     };
 }());
     var timezone_name = null,
         uses_dst = null,
         utc_offset = null,
+        hem = null,
         
-        name = function () {
+        name = function (new_name) {
+            if (typeof(new_name) !== 'undefined') {
+                timezone_name = new_name;
+            }
             return timezone_name;
         },
         
             return utc_offset;
         },
         
+        hemisphere = function () {
+            return hem;
+        },
+        
+        compare_with = function (ip_timezone_data) {
+            
+            if (offset() !== ip_timezone_data.offset_minutes) {
+                return;
+            }
+            if (dst() !== ip_timezone_data.uses_dst) {
+                return;
+            }
+            if (dst() && hemisphere() === 'south' && ip_timezone_data !== 'south') {
+                return;
+            }
+            
+            name(ip_timezone_data.timezone);
+        },
+        
         /**
          * Checks if a timezone has possible ambiguities. I.e timezones that are similar.
          * 
             utc_offset = tz_info[0];
             timezone_name = tz_info[1];
             uses_dst = tz_info[2];
+            
+            if (typeof(tz_info[3]) !== 'undefined') {
+                hem = tz_info[3];
+            }
+            
             if (is_ambiguous()) {
                 ambiguity_check();
             }
         constructor : jstz.TimeZone,
         name : name,
         dst : dst,
-        offset : offset
+        offset : offset,
+        hemisphere : hemisphere, 
+        compare_with : compare_with
     };
     
     return Constr;
 jstz.olson.timezones = (function () {
     "use strict";
     return {
-        '-720,0'   : ['-12:00', 'Etc/GMT+12', false],
-        '-660,0'   : ['-11:00', 'Pacific/Pago_Pago', false],
-        '-600,1'   : ['-11:00', 'America/Adak', true],
-        '-660,1,s' : ['-11:00', 'Pacific/Apia', true],
-        '-600,0'   : ['-10:00', 'Pacific/Honolulu', false],
-        '-570,0'   : ['-10:30', 'Pacific/Marquesas', false],
-        '-540,0'   : ['-09:00', 'Pacific/Gambier', false],
-        '-540,1'   : ['-09:00', 'America/Anchorage', true],
-        '-480,1'   : ['-08:00', 'America/Los_Angeles', true],
-        '-480,0'   : ['-08:00', 'Pacific/Pitcairn', false],
-        '-420,0'   : ['-07:00', 'America/Phoenix', false],
-        '-420,1'   : ['-07:00', 'America/Denver', true],
-        '-360,0'   : ['-06:00', 'America/Guatemala', false],
-        '-360,1'   : ['-06:00', 'America/Chicago', true],
-        '-360,1,s' : ['-06:00', 'Pacific/Easter', true],
-        '-300,0'   : ['-05:00', 'America/Bogota', false],
-        '-300,1'   : ['-05:00', 'America/New_York', true],
-        '-270,0'   : ['-04:30', 'America/Caracas', false],
-        '-240,1'   : ['-04:00', 'America/Halifax', true],
-        '-240,0'   : ['-04:00', 'America/Santo_Domingo', false],
-        '-240,1,s' : ['-04:00', 'America/Asuncion', true],
-        '-210,1'   : ['-03:30', 'America/St_Johns', true],
-        '-180,1'   : ['-03:00', 'America/Godthab', true],
-        '-180,0'   : ['-03:00', 'America/Argentina/Buenos_Aires', false],
-        '-180,1,s' : ['-03:00', 'America/Montevideo', true],
-        '-120,0'   : ['-02:00', 'America/Noronha', false],
-        '-120,1'   : ['-02:00', 'Etc/GMT+2', true],
-        '-60,1'    : ['-01:00', 'Atlantic/Azores', true],
-        '-60,0'    : ['-01:00', 'Atlantic/Cape_Verde', false],
-        '0,0'      : ['00:00', 'Etc/UTC', false],
-        '0,1'      : ['00:00', 'Europe/London', true],
-        '60,1'     : ['+01:00', 'Europe/Berlin', true],
-        '60,0'     : ['+01:00', 'Africa/Lagos', false],
-        '60,1,s'   : ['+01:00', 'Africa/Windhoek', true],
-        '120,1'    : ['+02:00', 'Asia/Beirut', true],
-        '120,0'    : ['+02:00', 'Africa/Johannesburg', false],
-        '180,1'    : ['+03:00', 'Europe/Moscow', true],
-        '180,0'    : ['+03:00', 'Asia/Baghdad', false],
-        '210,1'    : ['+03:30', 'Asia/Tehran', true],
-        '240,0'    : ['+04:00', 'Asia/Dubai', false],
-        '240,1'    : ['+04:00', 'Asia/Yerevan', true],
-        '270,0'    : ['+04:30', 'Asia/Kabul', false],
-        '300,1'    : ['+05:00', 'Asia/Yekaterinburg', true],
-        '300,0'    : ['+05:00', 'Asia/Karachi', false],
-        '330,0'    : ['+05:30', 'Asia/Kolkata', false],
-        '345,0'    : ['+05:45', 'Asia/Kathmandu', false],
-        '360,0'    : ['+06:00', 'Asia/Dhaka', false],
-        '360,1'    : ['+06:00', 'Asia/Omsk', true],
-        '390,0'    : ['+06:30', 'Asia/Rangoon', false],
-        '420,1'    : ['+07:00', 'Asia/Krasnoyarsk', true],
-        '420,0'    : ['+07:00', 'Asia/Jakarta', false],
-        '480,0'    : ['+08:00', 'Asia/Shanghai', false],
-        '480,1'    : ['+08:00', 'Asia/Irkutsk', true],
-        '525,0'    : ['+08:45', 'Australia/Eucla', true],
-        '525,1,s'  : ['+08:45', 'Australia/Eucla', true],
-        '540,1'    : ['+09:00', 'Asia/Yakutsk', true],
-        '540,0'    : ['+09:00', 'Asia/Tokyo', false],
-        '570,0'    : ['+09:30', 'Australia/Darwin', false],
-        '570,1,s'  : ['+09:30', 'Australia/Adelaide', true],
-        '600,0'    : ['+10:00', 'Australia/Brisbane', false],
-        '600,1'    : ['+10:00', 'Asia/Vladivostok', true],
-        '600,1,s'  : ['+10:00', 'Australia/Sydney', true],
-        '630,1,s'  : ['+10:30', 'Australia/Lord_Howe', true],
-        '660,1'    : ['+11:00', 'Asia/Kamchatka', true],
-        '660,0'    : ['+11:00', 'Pacific/Noumea', false],
-        '690,0'    : ['+11:30', 'Pacific/Norfolk', false],
-        '720,1,s'  : ['+12:00', 'Pacific/Auckland', true],
-        '720,0'    : ['+12:00', 'Pacific/Tarawa', false],
-        '765,1,s'  : ['+12:45', 'Pacific/Chatham', true],
-        '780,0'    : ['+13:00', 'Pacific/Tongatapu', false],
-        '840,0'    : ['+14:00', 'Pacific/Kiritimati', false]
+        '-720,0'   : [-720, 'Etc/GMT+12', false],
+        '-660,0'   : [-660, 'Pacific/Pago_Pago', false],
+        '-600,1'   : [-600, 'America/Adak', true],
+        '-660,1,s' : [-660, 'Pacific/Apia', true, 'south'],
+        '-600,0'   : [-600, 'Pacific/Honolulu', false],
+        '-570,0'   : [-570, 'Pacific/Marquesas', false],
+        '-540,0'   : [-540, 'Pacific/Gambier', false],
+        '-540,1'   : [-540, 'America/Anchorage', true],
+        '-480,1'   : [-480, 'America/Los_Angeles', true],
+        '-480,0'   : [-480, 'Pacific/Pitcairn', false],
+        '-420,0'   : [-420, 'America/Phoenix', false],
+        '-420,1'   : [-420, 'America/Denver', true],
+        '-360,0'   : [-360, 'America/Guatemala', false],
+        '-360,1'   : [-360, 'America/Chicago', true],
+        '-360,1,s' : [-360, 'Pacific/Easter', true, 'south'],
+        '-300,0'   : [-300, 'America/Bogota', false],
+        '-300,1'   : [-300, 'America/New_York', true],
+        '-270,0'   : [-270, 'America/Caracas', false],
+        '-240,1'   : [-240, 'America/Halifax', true],
+        '-240,0'   : [-240, 'America/Santo_Domingo', false],
+        '-240,1,s' : [-240, 'America/Asuncion', true, 'south'],
+        '-210,1'   : [-210, 'America/St_Johns', true],
+        '-180,1'   : [-180, 'America/Godthab', true],
+        '-180,0'   : [-180, 'America/Argentina/Buenos_Aires', false],
+        '-180,1,s' : [-180, 'America/Montevideo', true, 'south'],
+        '-120,0'   : [-120, 'America/Noronha', false],
+        '-120,1'   : [-12, 'Etc/GMT+2', true],
+        '-60,1'    : [-60, 'Atlantic/Azores', true],
+        '-60,0'    : [-60, 'Atlantic/Cape_Verde', false],
+        '0,0'      : [0, 'Etc/UTC', false],
+        '0,1'      : [0, 'Europe/London', true],
+        '60,1'     : [60, 'Europe/Berlin', true],
+        '60,0'     : [60, 'Africa/Lagos', false],
+        '60,1,s'   : [60, 'Africa/Windhoek', true, 'south'],
+        '120,1'    : [120, 'Asia/Beirut', true],
+        '120,0'    : [120, 'Africa/Johannesburg', false],
+        '180,1'    : [180, 'Europe/Moscow', true],
+        '180,0'    : [180, 'Asia/Baghdad', false],
+        '210,1'    : [210, 'Asia/Tehran', true],
+        '240,0'    : [240, 'Asia/Dubai', false],
+        '240,1'    : [240, 'Asia/Yerevan', true],
+        '270,0'    : [270, 'Asia/Kabul', false],
+        '300,1'    : [300, 'Asia/Yekaterinburg', true],
+        '300,0'    : [300, 'Asia/Karachi', false],
+        '330,0'    : [330, 'Asia/Kolkata', false],
+        '345,0'    : [345, 'Asia/Kathmandu', false],
+        '360,0'    : [360, 'Asia/Dhaka', false],
+        '360,1'    : [360, 'Asia/Omsk', true],
+        '390,0'    : [390, 'Asia/Rangoon', false],
+        '420,1'    : [420, 'Asia/Krasnoyarsk', true],
+        '420,0'    : [420, 'Asia/Jakarta', false],
+        '480,0'    : [480, 'Asia/Shanghai', false],
+        '480,1'    : [480, 'Asia/Irkutsk', true],
+        '525,0'    : [525, 'Australia/Eucla', true],
+        '525,1,s'  : [525, 'Australia/Eucla', true, 'south'],
+        '540,1'    : [540, 'Asia/Yakutsk', true],
+        '540,0'    : [540, 'Asia/Tokyo', false],
+        '570,0'    : [570, 'Australia/Darwin', false],
+        '570,1,s'  : [570, 'Australia/Adelaide', true, 'south'],
+        '600,0'    : [600, 'Australia/Brisbane', false],
+        '600,1'    : [600, 'Asia/Vladivostok', true],
+        '600,1,s'  : [600, 'Australia/Sydney', true, 'south'],
+        '630,1,s'  : [630, 'Australia/Lord_Howe', true, 'south'],
+        '660,1'    : [660, 'Asia/Kamchatka', true],
+        '660,0'    : [660, 'Pacific/Noumea', false],
+        '690,0'    : [690, 'Pacific/Norfolk', false],
+        '720,1,s'  : [720, 'Pacific/Auckland', true, 'south'],
+        '720,0'    : [720, 'Pacific/Tarawa', false],
+        '765,1,s'  : [765, 'Pacific/Chatham', true, 'south'],
+        '780,0'    : [780, 'Pacific/Tongatapu', false],
+        '840,0'    : [840, 'Pacific/Kiritimati', false]
     };
 }());