Commits

Jon Nylander  committed b279b83

Moved a few variables to constants. Included a minified version.

  • Participants
  • Parent commits 48100c2

Comments (0)

Files changed (3)

File detect_timezone.js

  */
 
 var ONE_HOUR_IN_MINUTES = 1000 * 60;
-
+var HEMISPHERE_SOUTH = 'SOUTH';
+var HEMISPHERE_NORTH = 'NORTH';
+var HEMISPHERE_UNKNOWN = 'N/A';
 var olsen = {}
 
 /**
  *
  * @constructor
  * @param {string} offset - for example '-11:00'
- * @param {string} olsen_tz - the olsen Identifier, such as "America/Denver"
+ * @param {string} tz_name - a name for the time zone
  * @param {boolean} uses_dst - flag for whether the time zone somehow cares about daylight savings.
  */
 function NonOlsenTimeZone(offset, tz_name, uses_dst) {
  * Gets the offset in minutes from UTC for a certain date.
  * 
  * @param date
- * @returns {Number}
+ * @returns {number}
  */
 function get_date_offset(date) {
   var gmt_version = date.toGMTString();
 	var diff = january_offset - june_offset;
 
 	if (diff < 0) {
-	    return {'utc_offset' : january_offset, 'dst':	 1, 'hemisphere' : 'north'}
+	    return {'utc_offset' : january_offset,
+	    		'dst':	1,
+	    		'hemisphere' : HEMISPHERE_NORTH}
 	}
 	else if (diff > 0) {
-        return {'utc_offset' : june_offset, 'dst' : 1, 'hemisphere' : 'south'}
+        return {'utc_offset' : june_offset,
+        		'dst' : 1,
+        		'hemisphere' : HEMISPHERE_SOUTH}
 	}
 
-    return {'utc_offset' : january_offset, 'dst': 0, 'hemisphere' : 'N/A'}
+    return {'utc_offset' : january_offset, 
+    		'dst': 0, 
+    		'hemisphere' : HEMISPHERE_UNKNOWN}
 }
 
 function get_january_offset() {
 	
 	var hemisphere_suffix = ''
 		
-	if (timezone_key_info.hemisphere == 'south') {
+	if (timezone_key_info.hemisphere == HEMISPHERE_SOUTH) {
 		hemisphere_suffix = ',s';
 	}
 	

File detect_timezone.min.js

+var ONE_HOUR_IN_MINUTES=6E4,HEMISPHERE_SOUTH="SOUTH",HEMISPHERE_NORTH="NORTH",HEMISPHERE_UNKNOWN="N/A",olsen={};
+olsen.timezones={"-720,0":new NonOlsenTimeZone("-12:00","International Datetime West",false),"-660,0":new TimeZone("-11:00","Pacific/Midway",false),"-660,1,s":new TimeZone("-11:00","Pacific/Apia",true),"-600,0":new TimeZone("-10:00","Pacific/Honolulu",false),"-540,1":new TimeZone("-09:00","America/Anchorage",true),"-480,1":new TimeZone("-08:00","America/Vancouver",true),"-420,0":new TimeZone("-07:00","America/Phoenix",false),"-420,1":new TimeZone("-07:00","America/Denver",true),"-360,0":new TimeZone("-06:00",
+"America/Regina",false),"-360,1":new TimeZone("-06:00","America/Chicago",true),"-300,0":new TimeZone("-05:00","America/Atikokan",false),"-300,1":new TimeZone("-05:00","America/Montreal",true),"-270,0":new TimeZone("-04:30","America/Caracas",false),"-240,1":new TimeZone("-04:00","America/Halifax",true),"-240,0":new TimeZone("-04:00","America/Manaus",false),"-240,1,s":new TimeZone("-04:00","America/Asuncion",true),"-210,1":new TimeZone("-03:30","America/St_Johns",true),"-180,1":new TimeZone("-03:00",
+"America/Godthab",true),"-180,0":new TimeZone("-03:00","America/Argentina/Buenos_Aires,",false),"-180,1,s":new TimeZone("-03:00","America/Montevideo",true),"-120,0":new TimeZone("-02:00","America/Noronha",true),"-120,1":new NonOlsenTimeZone("-02:00","Mid Atlantic",true),"-60,1":new TimeZone("-01:00","Atlantic/Azores",true),"-60,0":new TimeZone("-01:00","Atlantic/Cape_Verde",false),"0,0":new TimeZone("00:00","Atlantic/Reykjavik",false),"0,1":new TimeZone("00:00","Europe/Dublin",true),"60,1":new TimeZone("+01:00",
+"Europe/Amsterdam",true),"60,0":new TimeZone("+01:00","Africa/Porto-Novo",false),"60,1,s":new TimeZone("-01:00","Africa/Windhoek",true),"120,1":new TimeZone("+02:00","Asia/Beirut",true),"120,0":new TimeZone("+02:00","Africa/Harare",false),"180,1":new TimeZone("+03:00","Europe/Moscow",true),"180,0":new TimeZone("+03:00","Asia/Baghdad",false),"210,1":new TimeZone("+03:30","Asia/Tehran",true),"240,0":new TimeZone("+04:00","Asia/Dubai",false),"240,1":new TimeZone("+04:00","Asia/Yerevan",true),"270,0":new TimeZone("+04:30",
+"Asia/Kabul",false),"300,1":new TimeZone("+05:00","Asia/Yekaterinburg",true),"300,0":new TimeZone("+05:00","Asia/Karachi",false),"330,0":new TimeZone("+05:30","Asia/Kolkata",false),"345,0":new TimeZone("+05:45","Asia/Kathmandu",false),"360,0":new TimeZone("+06:00","Asia/Dhaka",false),"360,1":new TimeZone("+06:00","Asia/Novosibirsk",true),"390,0":new TimeZone("+06:30","Asia/Rangoon",false),"420,1":new TimeZone("+07:00","Asia/Krasnoyarsk",true),"420,0":new TimeZone("+07:00","Asia/Bangkok",false),"480,0":new TimeZone("+08:00",
+"Asia/Shanghai",false),"480,1":new TimeZone("+08:00","Asia/Irkutsk",true),"540,1":new TimeZone("+09:00","Asia/Yakutsk",true),"540,0":new TimeZone("+09:00","Asia/Seoul",false),"570,0":new TimeZone("+09:30","Australia/Darwin",false),"570,1,s":new TimeZone("+09:30","Australia/Adelaide",true),"600,0":new TimeZone("+10:00","Australia/Brisbane",false),"600,1":new TimeZone("+10:00","Asia/Vladivostok",true),"600,1,s":new TimeZone("+10:00","Australia/Melbourne",true),"660,1":new TimeZone("+11:00","Asia/Magadan Magadan, New Caledonia",
+true),"660,0":new TimeZone("+11:00","Pacific/Ponape",false),"720,1,s":new TimeZone("+12:00","Pacific/Auckland",true),"720,0":new TimeZone("+12:00","Pacific/Fiji",false),"780,0":new TimeZone("+13:00","Pacific/Tongatapu",false)};
+olsen.dst_start_dates={"America/Denver":new Date(2011,2,13,3,0,0,0),"America/Chihuahua":new Date(2011,3,3,3,0,0,0),"America/Chicago":new Date(2011,2,13,3,0,0,0),"America/Mexico_City":new Date(2011,3,3,3,0,0,0),"America/Asuncion":new Date(2011,9,2,3,0,0,0),"America/Santiago":new Date(2011,9,9,3,0,0,0),"America/Cuiaba":new Date(2011,9,16,5,0,0,0),"America/Montevideo":new Date(2011,9,2,2,0,0,0),"America/Sao_Paolo":new Date(2011,9,16,5,0,0,0),"Europe/Minsk":new Date(2011,2,27,5,0,0,0),"Europe/Helsinki":new Date(2011,
+2,27,7,0,0,0),"Asia/Beirut":new Date(2011,2,27,13,0,0,0),"Asia/Amman":new Date(2011,3,1,3,0,0,0),"Asia/Jerusalem":new Date(2011,3,1,23,0,0,0),"Africa/Cairo":new Date(2011,3,29,5,0,0,0),"Asia/Yerevan":new Date(2011,2,27,4,0,0,0),"Asia/Baku":new Date(2011,2,27,9,0,0,0),"Pacific/Auckland":new Date(2011,8,26,7,0,0,0),"Pacific/Fiji":new Date(2010,11,29,23,0,0,0)};
+olsen.ambiguity_list={"America/Denver":["America/Denver","America/Chihuahua"],"America/Chicago":["America/Chicago","America/Mexico_City"],"America/Asuncion":["America/Asuncion","America/Santiago","America/Cuiaba"],"America/Montevideo":["America/Montevideo","America/Sao_Paolo"],"Asia/Beirut":["Europe/Minsk","Europe/Helsinki","Asia/Beirut","Asia/Amman","Asia/Jerusalem","Africa/Cairo"],"Asia/Yerevan":["Asia/Yerevan","Asia/Baku"],"Pacific/Auckland":["Pacific/Auckland","Pacific/Fiji"]};
+function TimeZone(a,b,c){this.utc_offset=a;this.olsen_tz=b;this.uses_dst=c}TimeZone.prototype.display=function(){this.ambiguity_check();var a="<b>UTC-offset</b>: "+this.utc_offset+"<br/>";a+="<b>Olsen database name</b>: "+this.olsen_tz+"<br/>";a+="<b>Daylight Savings</b>: "+(this.uses_dst?"yes":"no")+"<br>";return a};
+TimeZone.prototype.ambiguity_check=function(){var a=olsen.ambiguity_list[this.olsen_tz];if(typeof a!="undefined")for(var b=a.length,c=0;c<b;c++){var d=a[c];if(date_is_dst(olsen.dst_start_dates[d])){this.olsen_tz=d;break}}};function NonOlsenTimeZone(a,b,c){this.utc_offset=a;this.tz_name=b;this.uses_dst=c}
+NonOlsenTimeZone.prototype.display=function(){var a='<b style="color: red;">This timezone is not mapped in the Olsen database</b><br/>';a+="<b>UTC-offset</b>: "+this.utc_offset+"<br/>";a+="<b>Time zone name</b>: "+this.tz_name+"<br/>";a+="<b>Daylight Savings</b>: "+(this.uses_dst?"yes":"no")+"<br/>";return a};function date_is_dst(a){var b=a.getMonth()>5?get_june_offset():get_january_offset();a=get_date_offset(a);return b-a!=0}
+function get_date_offset(a){var b=a.toGMTString();b=b.substring(0,b.lastIndexOf(" "));return(a-new Date(b))/ONE_HOUR_IN_MINUTES}function get_timezone_info(){var a=get_january_offset(),b=get_june_offset(),c=a-b;if(c<0)return{utc_offset:a,dst:1,hemisphere:HEMISPHERE_NORTH};else if(c>0)return{utc_offset:b,dst:1,hemisphere:HEMISPHERE_SOUTH};return{utc_offset:a,dst:0,hemisphere:HEMISPHERE_UNKNOWN}}function get_january_offset(){return get_date_offset(new Date(2011,0,1,0,0,0,0))}
+function get_june_offset(){return get_date_offset(new Date(2011,5,1,0,0,0,0))}function determine_timezone(){var a=get_timezone_info(),b="";if(a.hemisphere==HEMISPHERE_SOUTH)b=",s";a=a.utc_offset+","+a.dst+b;return{timezone:olsen.timezones[a],key:a}}
+function show_timezone_info(){var a=determine_timezone();response_text="No timezone found for "+a.key;response_text=typeof a.timezone=="undefined"?"No timezone found for "+a.key:a.timezone.display();document.getElementById("tz_info").innerHTML=response_text}onload=show_timezone_info;
   Continued by jon at pageloom dot com as jsTimezoneDetect.
   Open source on bitbucket.org.
   -->
-  <script type="text/javascript" src='detect_timezone.js'></script>
+  <script type="text/javascript" src='detect_timezone.min.js'></script>
 </head>
 
 <body>