SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with world clock

    dear everyone, i'm having headaches.

    i'm attempting to get a world clock working accuratley, at the moment it is, it is working, but it isn't taking into account daylight savings times across the world, the clock will only ever need to show the time in 4 major cities around the world, but it needs to show this time including wether ir not the city is in dst.

    this is my code as is:

    Code:
    <script language="JavaScript" type="text/javascript">
    function dozero(num) {
      if (num < 10) {
      	num = "0"+num;
    	return num;
      } else {
    	return num;
      }
    }
    function sivamtime() {
      now=new Date();
      hour=now.getUTCHours();
      hour1 = dozero(hour + 1);
      hour2 = dozero(hour - 5);
      hour3 = dozero(hour + 9);
      hour4 = dozero(hour - 7);
      mini=now.getUTCMinutes();
      sec=now.getUTCSeconds();
      time = hour1 + ":" + mini + ":" + sec;
      time2 = hour2 + ":" + mini + ":" + sec;
      time3 = hour3 + ":" + mini + ":" + sec;
      time4 = hour4 + ":" + mini + ":" + sec;
      var htime1 = document.getElementById("theTime");
      var htime2 = document.getElementById("theTime2");
      var htime3 = document.getElementById("theTime3");
      var htime4 = document.getElementById("theTime4");
      htime1.innerHTML = time;
      htime2.innerHTML = time2;
      htime3.innerHTML = time3;
      htime4.innerHTML = time4;
      setTimeout("sivamtime()", 1000);
    }
    window.onload = sivamtime;
    
    </script>
    (i know it's ugly, it's not my code, just my problem to fix )

    i'm basically asking is it posibble to stick something into this which says:

    "ok, london's dst is XXX
    new yorks is XXX
    san francisco's is XXX &
    Sydney's is XXX"

    it'll check if we are in dst for that time zone and ajust accordingly??? is that possible & how?

    many many thanks.

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Tricky because afaik there is no standard formula to tell if a given city is in (or even uses) DST.

    A quick google on [daylight savings time dates] found this page which lists some of the dates:
    http://webexhibits.org/daylightsaving/g.html

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't need to know if they use dst, just if they are in it, it will only ever be four citys, and i'm pretty sure all of them at some point in the year use daylights saving time, is there a way to translate this into the code?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, here are the rules for those 4 cities (copied from that page I listed)

    UK
    Start: Last Sunday in March at 1 am UTC
    End: Last Sunday in October at 1 am UTC

    USA
    Start: First Sunday in April
    End: Last Sunday in October

    Australia
    Start: Last Sunday in October
    End: Last Sunday in March

    So, make a function that checks the date - see if you are between those dates if so adjust accordingly.

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmmm, i have a bit of a problem. i have the start and end times of daylights savings time for the four cities i'm working with (London, New York, San Francisco & Sydney) but it seems that whoever decided to "invent" dst is an idiot. lol. they all start or end on "the last sunday" of a given month. so i now need to work out

    a) is today before or after the last sunday of month XXX?

    b) is today a dst day for this city?

    .... hmm i can't work out the logic. i need to know if it sits in between the start and end of dst right?

    here is my data so far:

    London:
    Start - Last Sunday of March
    End - Last Sunday of October

    New York & San Francisco
    Start - Last Sunday of April
    End - Last Sunday of October

    Sydney
    Start - Last Sunday of October
    End - Last Sunday of March

    how can i translate that into a "catch" of sorts to work out the real time including dst for my four cities?

  6. #6
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol we posted at the same time!!!!

    ok so how would a function like that work? based on the today date? how does it translate it to each city in turn???

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i suppose what i'm trying to work out is how would i compare two dates then? can i convert it to a number or something? actually it's compairing a date (todays date) with a date range isn't it?

    so like (excuse this, i'm brain-dumping)

    if ((todaysDate >= startDST) && (todaysDate <= endDST)) {
    thisCityTime +1;
    }

    would that work??????

  8. #8
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but then i need to work out startDST & endDST i need to establish using code what the last sunday of the month is. how woud i do that????

    i think this is the the bit that is bugging me.....

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is some code that's not complete but it's a good starting point for you.

    Code:
    <html><head>
    <script type="text/javascript">
    function inDST(city, dateNow) {
    	var dstStart;
    	var dstEnd;
    	var bInDST = false;
    	var dateTest;
    	
    	switch( city.toLowerCase() ) {
    		case "london":
    			dstStart = getLastSunday(dateNow.getYear(), 2);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "new york":
    			dstStart = getLastSunday(dateNow.getYear(), 3);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "san francisco":
    			dstStart = getLastSunday(dateNow.getYear(), 3);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "sydney":
    		
    			// we'll need to change the year of start or end of DST depending on which month we're in
    			var yearStart = 0;
    			var yearEnd = 0;
    			if( dateNow.getMonth() > 8 ) {
    				yearEnd = 1;
    			} else {
    				yearStart = -1;
    			}
    			dstStart = getLastSunday(dateNow.getYear() + yearStart, 9);
    			dstEnd = getLastSunday(dateNow.getYear() + yearEnd, 2);
    			break;
    	
    	}
    
    	alert("Starts " + dstStart + ", Ends " + dstEnd);
    
    	if( dateNow >= dstStart && dateNow <= dstEnd ) {
    		bInDST = true;
    	}
    
    	return bInDST;
    
    }
    function getLastSunday(year,month) {
    	var dLastSunday;
    	var dateTest;
    	for( var i=getDaysPerMonth(year,month); i > 0; i-- ) {
    		dateTest = new Date(year, month, i);
    		if( dateTest.getDay() == 0 ) {
    			dLastSunday = dateTest;
    			break;
    		}
    	}
    	return dLastSunday;
    }
    function getDaysPerMonth(year,month) {
    	var daysPerMonth = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    	var nDays;
    	nDays = daysPerMonth[month];
    	if( nDays == -1 ) {
    		if( year % 4 == 0 && year % 100 != 0 ) {
    			nDays = 29;
    		} else {
    			nDays = 28;
    		}
    	}
    	return nDays;
    }
    </script>
    </head>
    <body>
    <input type="button" value="London" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="New York" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="San Francisco" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="Sydney" onclick="alert(inDST(this.value, new Date()));">
    </body>
    </html>

  10. #10
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jimfraser View Post
    Here is some code that's not complete but it's a good starting point for you.

    Code:
    <html><head>
    <script type="text/javascript">
    function inDST(city, dateNow) {
    	var dstStart;
    	var dstEnd;
    	var bInDST = false;
    	var dateTest;
    	
    	switch( city.toLowerCase() ) {
    		case "london":
    			dstStart = getLastSunday(dateNow.getYear(), 2);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "new york":
    			dstStart = getLastSunday(dateNow.getYear(), 3);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "san francisco":
    			dstStart = getLastSunday(dateNow.getYear(), 3);
    			dstEnd = getLastSunday(dateNow.getYear(), 9);
    			break;
    		case "sydney":
    		
    			// we'll need to change the year of start or end of DST depending on which month we're in
    			var yearStart = 0;
    			var yearEnd = 0;
    			if( dateNow.getMonth() > 8 ) {
    				yearEnd = 1;
    			} else {
    				yearStart = -1;
    			}
    			dstStart = getLastSunday(dateNow.getYear() + yearStart, 9);
    			dstEnd = getLastSunday(dateNow.getYear() + yearEnd, 2);
    			break;
    	
    	}
    
    	alert("Starts " + dstStart + ", Ends " + dstEnd);
    
    	if( dateNow >= dstStart && dateNow <= dstEnd ) {
    		bInDST = true;
    	}
    
    	return bInDST;
    
    }
    function getLastSunday(year,month) {
    	var dLastSunday;
    	var dateTest;
    	for( var i=getDaysPerMonth(year,month); i > 0; i-- ) {
    		dateTest = new Date(year, month, i);
    		if( dateTest.getDay() == 0 ) {
    			dLastSunday = dateTest;
    			break;
    		}
    	}
    	return dLastSunday;
    }
    function getDaysPerMonth(year,month) {
    	var daysPerMonth = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    	var nDays;
    	nDays = daysPerMonth[month];
    	if( nDays == -1 ) {
    		if( year % 4 == 0 && year % 100 != 0 ) {
    			nDays = 29;
    		} else {
    			nDays = 28;
    		}
    	}
    	return nDays;
    }
    </script>
    </head>
    <body>
    <input type="button" value="London" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="New York" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="San Francisco" onclick="alert(inDST(this.value, new Date()));">
    <input type="button" value="Sydney" onclick="alert(inDST(this.value, new Date()));">
    </body>
    </html>
    Thanks for this. have somebody completed DST code?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •