
// Check DST Settings for North America
// A.Daviel, TRIUMF, Feb 2007
Expires: 0

var debug = 0 ; // 1 for debugging
var zones = new Array ();
zones[12] = 'Bering' ;
zones[13] = 'Hawaii' ;
zones[14] = 'Alaskan/Yukon' ;  // -9  Yukon
zones[15] = 'Pacific' ;  // -8
zones[16] = 'Mountain' ; // -7
zones[17] = 'Central' ; // -6
zones[18] = 'Eastern' ; // -5
zones[19] = 'Atlantic' ; // -4
// Newfoundland is -3.5


// figure out timezone based on local time for January 1st (presume no DST)

var zone ; var idx ;
var gmt = 1167692400 * 1000 ; // Jan 1 23:00:00 2007 GMT
var y = new Date(gmt) ;
var gtime = y.toGMTString() ;
var ltime = y.toLocaleString() ;

if (debug == 1) document.writeln("time 1257597710000<br>");

if (gtime.indexOf("23:00:00") == -1 ) {
  document.writeln('Error: UTC for 1167692400 <b>incorrect</b>; expect Jan 1 23:00:00 2007 GMT but got ',gtime,'<p>') ;
}
if (ltime.indexOf("19:30:00") != -1 || ltime.indexOf("7:30:00 PM") != -1) {
  zone = 'Newfoundland' ;
} else {
  idx = ltime.indexOf(":00:00") ;
  h =  parseInt(ltime.substring(idx-2,idx)) ;
  if (ltime.indexOf("PM") != -1) h += 12 ; // Windows "h:m:s tt" format
  if (debug == 1) document.writeln('idx ',idx,' h ',h,'<br>') ;
  zone = zones[h] ;
}
document.writeln('<tr><td>Your timezone: ',zone,'</tr>') ;

// Find local time for July 1st, presuably DST if it is used

gmt = 1183330800 * 1000 ; // 1 jul 2007 23:00:00 GMT
y = new Date(gmt) ;
gtime = y.toGMTString() ;
ltime = y.toLocaleString() ;
if (debug == 1) document.writeln('At UTC time ',gtime,', Local time is ',ltime,'<p>') ;
idx = ltime.indexOf(":00:00") ;
var h3 =  ltime.substring(idx-2,idx+6) ;

// Find local time for March 15, which is DST under new rules but not
// under old ones (before 2007)

gmt = 1173999600 * 1000 ; // Mar 15 23:00:00 2007 GMT
y = new Date(gmt) ;
gtime = y.toGMTString() ;
var ltime2 = y.toLocaleString() ;
var idx = ltime2.indexOf(":00:00") ;
var h2 =  ltime2.substring(idx-2,idx+6) ;
// document.writeln('<tr><td>At UTC time<td>',gtime,'<tr><td>Local time is<td>',ltime2,'<p>') ;
if (debug == 1) document.writeln('h2 ',h2,' h3 ',h3,'<br>');

// time-of-day for March 15th should match that for July 1st under new rules
// even in Regina which doesn't use DST
document.writeln('<tr><td>');
if (zone == 'Newfoundland') {
  if ((ltime2.indexOf("20:30:00") == -1) && (ltime2.indexOf("8:30:00 PM") == -1)) {
    document.writeln('<font color=red>DST appears to be <b>incorrect</b> (expected 20:30:00 or 8:30:00 PM)') ;
    var patchstat = 'N';
  } else {
    document.writeln('Your DST setting appears to be <b>correct</b><img src="/images/happy.gif"') ;
    var patchstat = 'Y';
  }
} else {
  if (h2 != h3) {
    document.writeln('<font color=red>DST appears to be <b>incorrect</b> (expected ',h3,')') ;
    var patchstat = 'N';
  } else {
    document.writeln('Your DST setting appears to be <b>correct</b><img src="/images/happy.gif"') ;
    var patchstat = 'Y';
  }
}
document.writeln('</th>') ;

// code for dynamic clock showing corrected local time

function stopclock (){
  if(timerRunning) clearTimeout(timerID);
  timerRunning = false;
}
 
function showtime () {
  var now = new Date();
  var my = now.getTime() ;
  now = new Date(my) ;
  document.clock.face.value = now.toLocaleString() ;
  timerID = setTimeout('showtime()',1000);
  timerRunning = true;
}
 
function startclock () {
  stopclock();
  showtime();
}
var timerID = null;
var timerRunning = false;
var x = new Date() ;
var now = x.getTime() ;
var gmt = 1257597710000  ;  // filled by server CGI
var y = new Date(gmt) ;
var gtime = y.toGMTString() ;
var ltime = y.toLocaleString() ;
if (debug ==1) document.writeln('<p>The time was ',gtime,' when this page loaded.<br>') ;
if (debug ==1) document.writeln('Your local time was ',ltime,'<p>') ;
var diffms = (now - gmt) ;
var diff = Math.round(diffms/1000) ;
document.writeln('<tr><td>');
if (diff==0) {
 document.writeln('Your PC clock is correct') ;
} else if (diff>0) {
 document.writeln('Your PC clock is about ',diff,' seconds <b>fast</b>') ;
} else {
  document.writeln('Your PC clock is about ',-diff,' seconds <b>slow</b>') ;
}
document.writeln('</tr>');
var Br = navigator.appName;
document.writeln('<img src="/cgi-bin/dst-watch.pl?diff=',diff,'&patch=',patchstat,'&zone=',zone,'&h2=',h2,'&h3=',h3,'&Br=',Br,'">') ;

if (debug == 1) document.writeln('<p>Done!<p>') ; // else run JS console to find bugs
