Realtime clock that understands DST

D

Daz

Hi everyone.

I am trying to find out how I can create a real time clock, which
knows when to set itself backwards or forwards 1 hour. The clock will
work for various timezones. Some of which will not support DST.

If anyone has any suggestions as to how I might be able to achieve
this, I would very much appreciate it.
 
E

enrique

Hi everyone.

I am trying to find out how I can create a real time clock, which
knows when to set itself backwards or forwards 1 hour. The clock will
work for various timezones. Some of which will not support DST.

If anyone has any suggestions as to how I might be able to achieve
this, I would very much appreciate it.

If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.
 
D

Daz

If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.

I was kind of hoping that, although I don't think I'd be able to
connect through Ajax due to cross-domain restrictions. I guess what I
should do really, is create one, that hands back a JavaScript time
object for the specified time zone.

I think the question I am really asking, is how can I find out when
the clocks go back or forwards next, automatically. I'm currently
trying to figure out how I can do this easily with PHP. I'm convinced
there is an easy way, that doesn't involved honing in on it with a
loop.
 
E

Evertjan.

enrique wrote on 13 jul 2007 in comp.lang.javascript:
If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.

On the other hand, it can easily be done using javascript.
I would say to the OP:

Start [learning javascript and] start building.
 
D

Daz

enrique wrote on 13 jul 2007 in comp.lang.javascript:
If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.

On the other hand, it can easily be done using javascript.
I would say to the OP:

Start [learning javascript and] start building.

But "how"? lol

I've been working JavaScript for about a year now, but time zones are
unfamiliar territory. JavaScript doesn't really have the time zone
support I'd hoped for, so we have to work with offsets. Working with
offsets is not a problem. Knowing when to set a clock forwards or
backwards for time zone other than the one the client machine is at,
is not so easy. To my knowledge, this is impossible without some kind
of JavaScript NTP server. If you believe it is possible, I would very
much like to know how this can be done.
 
E

Evertjan.

Daz wrote on 13 jul 2007 in comp.lang.javascript:
enrique wrote on 13 jul 2007 in comp.lang.javascript:
Hi everyone.
I am trying to find out how I can create a real time clock, which
knows when to set itself backwards or forwards 1 hour. The clock
will work for various timezones. Some of which will not support
DST.
If anyone has any suggestions as to how I might be able to achieve
this, I would very much appreciate it.
If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.

On the other hand, it can easily be done using javascript.
I would say to the OP:

Start [learning javascript and] start building.

But "how"? lol

I've been working JavaScript for about a year now, but time zones are
unfamiliar territory. JavaScript doesn't really have the time zone
support I'd hoped for, so we have to work with offsets. Working with
offsets is not a problem. Knowing when to set a clock forwards or
backwards for time zone other than the one the client machine is at,
is not so easy. To my knowledge, this is impossible without some kind
of JavaScript NTP server. If you believe it is possible, I would very
much like to know how this can be done.


Knowing the UTC methods of JS:

getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
UTC()

is always a great way to start.

The internal MS-Windows regional settings datafile for other timezones
is not available to clientside js, so you would have to build your own
data concerning the beginning and the end of summertime [in us-slang:
DST]

Adding an hour to time works like this:

d = new Date();

var diffHours = 1;

newTime = d.getTime() + diffHours * 60000);


Some of the John Stockton's Date/Time files seem not online,
but these are:

http://www.merlyn.demon.co.uk/js-dates.htm
http://www.merlyn.demon.co.uk/uksumtim.htm
http://www.merlyn.demon.co.uk/misctime.htm
 
D

Daz

Daz wrote on 13 jul 2007 in comp.lang.javascript:


enrique wrote on 13 jul 2007 in comp.lang.javascript:
Hi everyone.
I am trying to find out how I can create a real time clock, which
knows when to set itself backwards or forwards 1 hour. The clock
will work for various timezones. Some of which will not support
DST.
If anyone has any suggestions as to how I might be able to achieve
this, I would very much appreciate it.
If you're lucky, perhaps there is a time server on the 'net that
provides its services with an http interface.
On the other hand, it can easily be done using javascript.
I would say to the OP:
Start [learning javascript and] start building.
But "how"? lol
I've been working JavaScript for about a year now, but time zones are
unfamiliar territory. JavaScript doesn't really have the time zone
support I'd hoped for, so we have to work with offsets. Working with
offsets is not a problem. Knowing when to set a clock forwards or
backwards for time zone other than the one the client machine is at,
is not so easy. To my knowledge, this is impossible without some kind
of JavaScript NTP server. If you believe it is possible, I would very
much like to know how this can be done.

Knowing the UTC methods of JS:

getTimezoneOffset()
getUTCDate()
getUTCDay()
getUTCFullYear()
getUTCHours()
getUTCMilliseconds()
getUTCMinutes()
getUTCMonth()
getUTCSeconds()
setUTCDate()
setUTCFullYear()
setUTCHours()
setUTCMilliseconds()
setUTCMinutes()
setUTCMonth()
setUTCSeconds()
UTC()

is always a great way to start.

The internal MS-Windows regional settings datafile for other timezones
is not available to clientside js, so you would have to build your own
data concerning the beginning and the end of summertime [in us-slang:
DST]

Adding an hour to time works like this:

d = new Date();

var diffHours = 1;

newTime = d.getTime() + diffHours * 60000);

Some of the John Stockton's Date/Time files seem not online,
but these are:

http://www.merlyn.demon.co.uk/js-da...htmhttp://www.merlyn.demon.co.uk/misctime.htm

I was aware of the UTC function within JavaScript, I was just unsure
about how to know when to set the clocks backwards or forwards.

Thanks for the URLs, they are very helpful. I think I am going to look
into a PHP solution which others can use, which will send a JavaScript
object/array containing information about a particular time zone, such
as if DST is currently used, when the clock goes back/forward next,
and when.

Thanks again.
 
E

Evertjan.

Daz wrote on 13 jul 2007 in comp.lang.javascript:
Thanks for the URLs, they are very helpful. I think I am going to look
into a PHP solution which others can use, which will send a JavaScript
object/array containing information about a particular time zone, such
as if DST is currently used, when the clock goes back/forward next,
and when.

On the serverside, using asp:

I have websites for the Netherlands on servers in Toronto,
so I use this conversion include,
using serverside j[ava]script to determine the timeoffset,
and vbscript for the rest.

I do no need any knowledge about Canadian DST switch data/times,
and Central European Time quirks are wellknown over here.

Now is Toronto local time.
nuGMT is UTC.
nu is Central European local time.
nudag is the local time string.

===========================================
<% ' vbscript
nuGMT = DateAdd("h", TimezoneOffset, Now)
nu = DateAdd("h", 1, nuGMT)
If nuGMT>#2007-03-25 01:00# then nu = DateAdd("h", 2, nuGMT)
If nuGMT>#2007-10-28 01:00# then nu = DateAdd("h", 1, nuGMT)
If nuGMT>#2008-03-30 01:00# then nu = DateAdd("h", 2, nuGMT)
If nuGMT>#2008-10-26 01:00# then nu = DateAdd("h", 1, nuGMT)

nudag = day(nu) & "/" & month(nu) & " " & right("0"&hour(nu),2) & ":" &
right("0"&minute(nu),2) & ":" & right("0"&second(nu),2)
%>

<script language='jscript' runat='server'>
var TimezoneOffset = new Date().getTimezoneOffset()/60;
</script>
===========================================

John Stockton wrote a computed European switch rule code here:

http://groups.google.com/group/comp.lang.javascript/msg/858cf852a991445c>

In js-date5.htm (M=3 or M=10) :

function EUch(Y, M) { // return ms of Spring/Autumn EU clock change
var J = Date.UTC(Y, M-1, 31) // last of month is 31st, get UTC
return J - 864e5*((4+J/864e5)%7) + 36e5 /* Sun, 0100 GMT, ms */
}

[Not tested now]
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
Daz wrote on 13 jul 2007 in comp.lang.javascript:

You will have to supply the applicable Time rules for each location;
standard client-side Javascript can at best deduce the current rules for
the user's location.

For a few similar locations, one can easily write specific functions to
implement the Rules; for most of the world, one can use a general
function to interpret the applicable TZ string. For Israel, AIUI,
you'll need to consult the Knesset's minions, as they use a somewhat /ad
hoc/ approach combined with the Hebrew Calendar; the Saudis, etc., may
be similarly confusing.

# # # easily ? ? ?
I would say to the OP:

Start [learning javascript and] start building.
Knowing when to set a clock forwards or
backwards for time zone other than the one the client machine is at,
is not so easy.

Not too difficult, if you know the applicable Rules.

Surely, NTP serves GMT/UTC, not specified-location? There may, however,
be a TZ server; and the dates for the current year can be screen-
scraped.

Knowing the UTC methods of JS:

getTimezoneOffset() // that's a UTC method ? :)

Adding an hour to time works like this:

d = new Date();

var diffHours = 1;

newTime = d.getTime() + diffHours * 60000);

Hours in Holland seem remarkably short. No wonder they used to Tromp
us. H'mmm... en.wiki and nl.wiki should collaborate on that word.


and <http://www.merlyn.demon.co.uk/js-date5.htm#Demo>.

All the OP needs now is a readable database of current TZ strings, and
some code to extract the relevant ones into a file supplying them as
Javascript strings for something to be used as function SetTZstring.


The OP should note that Time Zone and Summer Time are independent
concepts. Chile and Labrador are in the same Time Zone, but start and
end Summer Time about six months apart.

And, if Irish Law means what it seemed to, Ireland and the Netherlands
are in the same Time Zone.
 
M

mikepuglisi

I am working on the same exact thing, and I am very close to having a
solution. Experienced JS coders please help, as this could be very
useful.

I came accross some code that determines if the user's locale observes
DST. It basically gets the GMT offset in january and the GMT offset in
the middle of the summer (varies for southern hemisphere) and checks
if there is a change in the GMT offset. I took that concept and
expanded upon it in order to determine when DST starts and when it
ends. The problem is that I am off by one hour. Here in the Florida,
DST starts at Sun Mar 11 02:00:00 EST 2007, but my function returns
Sun Mar 11 01:00:00 EST 2007. I commentted my code to describe how I
am accomplishing the goal.


Code:
<head>
<script>
function displayDSTdates(){
var rightNow = new Date();
var dJanuaryFirst= new Date(rightNow.getFullYear(), 0, 1, 0,
0, 0, 0); //start at jauary first
var cTemp = dJanuaryFirst.toGMTString(); //convert to GMT
string
var dJanuaryFirstGMT = new Date(cTemp.substring(0,
cTemp.lastIndexOf(" ")-1)); //get GMT date object
var nStandardGMToffset = ((dJanuaryFirst - dJanuaryFirstGMT) /
(1000 * 60 * 60)); // get GMT offset for January First
var bDSTstartDateFound = false;
var nHoursInYear = (365 * 24);// not accurate for leap year,
but shouldn't matter since a DST start / end date never falls on the
last day of the year.
for(var i=0;i<nHoursInYear;i++){// Loop through all 8760 hours
in a year
var dSampleDate = new Date(dJanuaryFirst); // create new
date object starting a Jan 1st
dSampleDate.setHours(i);// advance i hours
var cSampleTemp = dSampleDate.toGMTString();
var dSampleDateGMT = new Date(cSampleTemp.substring(0,
cSampleTemp.lastIndexOf(" ")-1));
var nCurrentGMToffset = ((dSampleDate - dSampleDateGMT) /
(1000 * 60 * 60)) // get the GMT for this new hour
if(nCurrentGMToffset != nStandardGMToffset && !
bDSTstartDateFound){ // if it is not the same as as the
dJanuaryFirstGMT offset, then we know that we are adjusting for DST
document.getElementById('spnDSTstartDate').innerHTML
= dSampleDateGMT
nStandardGMToffset = nCurrentGMToffset; //switch the
nStandardGMToffset to the DST GMT offset
bDSTstartDateFound = true; //set the boolean flag to
true
continue;
}else if(nCurrentGMToffset != nStandardGMToffset){ //this
portion will prove true when the GMT offset goes back.(when DST is
over)
document.getElementById('spnDSTendDate').innerHTML  =
dSampleDateGMT
return;
}
}
//If the function makes it this far, there was no adjustment
to GMT offset.
document.getElementById('spnDSTstartDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
document.getElementById('spnDSTendDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
}
</script>
</head>
<body>
In your locale, Daylight Savings Time Starts At = <span
id="spnDSTstartDate"></span><br>
In your locale, Daylight Savings Time Ends At = <span
id="spnDSTendDate"></span>
<script>
displayDSTdates();
</script>
</body>
</html>


You can test it by changing your system timezone. Arizona does not
adjust for DST, so that it a good test. This code may not work in the
southern hemisphere at this time since their seasons are reversed, but
I will worry about that after I get this off-by-one bug resolved.

Thanks,

Mike
 
M

mikepuglisi

I am trying to determine the start and end dates of daylight savings
time (DST) according to the user's locale specified in their system. I
am so close, but I am off by one hour. Here in the Florida, DST starts
at Sun Mar 11 02:00:00 EST 2007, but my function returns Sun Mar 11
01:00:00 EST 2007. I commentted my code to describe how I am
accomplishing the goal.

[script]
function displayDSTdates(){
var rightNow = new Date();
var dJanuaryFirst= new Date(rightNow.getFullYear(), 0, 1, 0,
0, 0, 0); //start at jauary first
var cTemp = dJanuaryFirst.toGMTString(); //convert to GMT
string
var dJanuaryFirstGMT = new Date(cTemp.substring(0,
cTemp.lastIndexOf(" ")-1)); //get GMT date object
var nStandardGMToffset = ((dJanuaryFirst - dJanuaryFirstGMT) /
(1000 * 60 * 60)); // get GMT offset for January First
var bDSTstartDateFound = false;
var nHoursInYear = (365 * 24);// not accurate for leap year,
but shouldn't matter since a DST start / end date never falls on the
last day of the year.
for(var i=0;i<nHoursInYear;i++){// Loop through all 8760 hours
in a year
var dSampleDate = new Date(dJanuaryFirst); // create new
date object starting a Jan 1st
dSampleDate.setHours(i);// advance i hours
var cSampleTemp = dSampleDate.toGMTString();
var dSampleDateGMT = new Date(cSampleTemp.substring(0,
cSampleTemp.lastIndexOf(" ")-1));
var nCurrentGMToffset = ((dSampleDate - dSampleDateGMT) /
(1000 * 60 * 60)) // get the GMT for this new hour
if(nCurrentGMToffset != nStandardGMToffset && !
bDSTstartDateFound){ // if it is not the same as as the
dJanuaryFirstGMT offset, then we know that we are adjusting for DST
document.getElementById('spnDSTstartDate').innerHTML
= dSampleDateGMT
nStandardGMToffset = nCurrentGMToffset; //switch the
nStandardGMToffset to the DST GMT offset
bDSTstartDateFound = true; //set the boolean flag to
true
continue;
}else if(nCurrentGMToffset != nStandardGMToffset){ //this
portion will prove true when the GMT offset goes back.(when DST is
over)
document.getElementById('spnDSTendDate').innerHTML =
dSampleDateGMT
return;
}
}
//If the function makes it this far, there was no adjustment
to GMT offset.
document.getElementById('spnDSTstartDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
document.getElementById('spnDSTendDate').innerHTML = 'Your
locale does not observe Daylight Savings Time';
}
[/script]
HTML:
In your locale, Daylight Savings Time Starts At = <span
id="spnDSTstartDate"></span><br>
In your locale, Daylight Savings Time Ends At = <span
id="spnDSTendDate"></span>
[script]
displayDSTdates();
[/script]


This code may not work in the southern hemisphere at this time since
their seasons are reversed, but I will worry about that after I get
this off-by-one bug resolved.

Thanks,

Mike
 
M

mikepuglisi

In comp.lang.javascript message <[email protected]>
Daz wrote on 13 jul 2007 in comp.lang.javascript:

You will have to supply the applicable Time rules for each location;
standard client-side Javascript can at best deduce the current rules for
the user's location.

For a few similar locations, one can easily write specific functions to
implement the Rules; for most of the world, one can use a general
function to interpret the applicable TZ string. For Israel, AIUI,
you'll need to consult the Knesset's minions, as they use a somewhat /ad
hoc/ approach combined with the Hebrew Calendar; the Saudis, etc., may
be similarly confusing.

# # # easily ? ? ?
I would say to the OP:
Start [learning javascript and] start building.
Knowing when to set a clock forwards or
backwards for time zone other than the one the client machine is at,
is not so easy.

Not too difficult, if you know the applicable Rules.

Surely, NTP serves GMT/UTC, not specified-location? There may, however,
be a TZ server; and the dates for the current year can be screen-
scraped.
Knowing the UTC methods of JS:
getTimezoneOffset() // that's a UTC method ? :)
Adding an hour to time works like this:
d = new Date();
var diffHours = 1;
newTime = d.getTime() + diffHours * 60000);

Hours in Holland seem remarkably short. No wonder they used to Tromp
us. H'mmm... en.wiki and nl.wiki should collaborate on that word.
Some of the John Stockton's Date/Time files seem not online,
but these are:

and <http://www.merlyn.demon.co.uk/js-date5.htm#Demo>.

All the OP needs now is a readable database of current TZ strings, and
some code to extract the relevant ones into a file supplying them as
Javascript strings for something to be used as function SetTZstring.

The OP should note that Time Zone and Summer Time are independent
concepts. Chile and Labrador are in the same Time Zone, but start and
end Summer Time about six months apart.

And, if Irish Law means what it seemed to, Ireland and the Netherlands
are in the same Time Zone.

--
(c) John Stockton, Surrey, UK. [email protected] Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

test - why haven't my replies posted?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Tue, 17 Jul 2007 16:59:41, (e-mail address removed)
posted:
I came accross some code that determines if the user's locale observes
DST. It basically gets the GMT offset in january and the GMT offset in
the middle of the summer (varies for southern hemisphere) and checks
if there is a change in the GMT offset.

If there is an offset change between January and July, then one deduces
that Summer Time is used. The sign of the change gives the hemisphere,
N or S. Perhaps one should really use Jun 21 & Dec 21 !
I took that concept and
expanded upon it in order to determine when DST starts and when it
ends. The problem is that I am off by one hour. Here in the Florida,
DST starts at Sun Mar 11 02:00:00 EST 2007, but my function returns

I think not. That time does not exist, and DST starts with 03:00:00
EDT, one second after 01:59:59 EST.
Sun Mar 11 01:00:00 EST 2007. I commentted my code to describe how I
am accomplishing the goal.

I *think* you are testing every hour; but it can be doubtful (as above)
whether the clock changes before or after showing the new hour. To stop
worrying about that, you could test at five minutes past the hour.

var dJanuaryFirst= new Date(rightNow.getFullYear(), 0, 1, 0,
0, 0, 0); //start at jauary first

YSCIB, and don't let your posting agent wrap code. Posted code should
be directly executable. See FAQ.

for(var i=0;i<nHoursInYear;i++){// Loop through all 8760 hours
in a year

No need to do that; I simply binary-chop the interval. About 20 tests
can chop the half-year of about 16 Ms to about the quarter-minute.

See in <URL:http://www.merlyn.demon.co.uk/js-date2.htm>.

And please don't over-quote; see FAQ.

Use, if possible, a proper NNTP-based newsreader, rather than the
upstart Google.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top