Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Checking for daylight saving

Reply
Thread Tools

Checking for daylight saving

 
 
RobG
Guest
Posts: n/a
 
      12-20-2007
I was investigating a function to determine whether daylight saving
was being observed on a particular date (given the platform's regional
settings) and came across a suggestion at merlyn.com to test the time
zone offset on a variety of dates to see if it changes. Based on
that, I developed the following checkDST() function which, as far as I
can tell, should be sufficient.

It checks either the date passed to it or the current date with 3
other dates, each 3 months further into the future. If any of their
time zones differs from the epoch then either:

true - daylight saving is being observed on the date, or

false - daylight saving is observed but not on the date

are returned. If no difference is found, undefined is returned. I
explicitly return undefined as a matter of style rather than let the
loop peter out and return undefined by default.

Can anyone suggest whether the algorithm is appropriate and if my
implementation is OK? It will fail if there are changes to time zone
offsets that are not related to daylight saving, and if changes occur
for a period of less than 3 months and fall within the dates tested.

As far as I know, there isn't anywhere that does that, but they might
be accommodated by an "acceptable range" test (say allow changes of up
to 5 minutes) and checking for symmetry - if the time zone is
different in x months time, check if it was also different (12 - x)
months ago, and check if it differed by the same amount taking into
consideration the acceptable range.


function checkDST(d) {

d = d || new Date();

var x = new Date(d);
var dt = d.getTimezoneOffset();
var xt;

for (var i=0; i<3; i++) {
x.setMonth(x.getMonth() + 3)
xt = x.getTimezoneOffset();

if (dt != xt) {
return dt < xt;
}
}
return undefined;
}

function showDST(){

var x = isDST(new Date());
var msg;

if (x === true) {
msg = 'Daylight saving is observed here ' +
'and is in force.';
} else if (x === false) {
msg = 'Daylight saving is observed here, ' +
'but not at the moment.';
} else {
msg = 'Daylight saving is not observed here at all.'
}
alert(x + ': ' + msg);
}

showDST();



--
Rob
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      12-20-2007
On Dec 20, 4:45 pm, Randy Webb <(E-Mail Removed)> wrote:
> RobG said the following on 12/20/2007 1:27 AM:
>
> <snip>
>
> > function checkDST(d) {

>
> <snip>
>
> > function showDST(){

>
> > var x = isDST(new Date());


That should of course be:

var x = checkDST(new Date());

>
> isDST is not defined. FF2.0
> Object Expected. IE7.
>
> Long day today?


bugger...


I changed the name of the function just before posting - does it work
for you? I can't mess with my regional settings for a little while
yet, I'll test it more when I can.


--
Rob

 
Reply With Quote
 
 
 
 
Evertjan.
Guest
Posts: n/a
 
      12-20-2007
Randy Webb wrote on 20 dec 2007 in comp.lang.javascript:

> Not for what Rob is wanting to test. The only way to know if it is
> getting it correct in different time zones is to change the time zone
> settings. It is just as trivial to do that for testing as changing the
> time/date. I just didn't do it when I was testing. Right click the
> clock on the taskbar in Windows>Adjust Date and Time>Time Zone tab.
> Change it, apply it, test it, repeat. There is also a setting there
> for automatically changing the time for DST. With it unchecked, the
> test gives incorrect results for me when testing my own time zone.
>
> "undefined: Daylight saving is not observed here at all. "
>
> And that is in Eastern Standard Time Zone in the US.


This should work, IMHO:

<script type='text/javascript'>

var d1 = new Date(2007,0,1).getTimezoneOffset()
var d2 = new Date(2007,6,1).getTimezoneOffset()

var t = 'This computer is set for a zone with'

if (d1==d2)
alert(t + 'out summertime offset');
else if (d1>d2)
alert(t + ' summertime offset in Northern hemisphere');
else
alert(t + ' summertime offset in Southern hemisphere');

</script>

IE7 and Central European Time Zone tested.

That such offset saves daylight is absolute nonsense,
though it may save energy consumption.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-20-2007
In comp.lang.javascript message <ecae4e75-9e61-4aa0-90ab-6c10978d781e@d4
g2000prg.googlegroups.com>, Wed, 19 Dec 2007 22:27:46, RobG
<(E-Mail Removed)> posted:
>I was investigating a function to determine whether daylight saving
>was being observed on a particular date (given the platform's regional
>settings) and came across a suggestion at merlyn.com to test the time

^^^^^^^^^^
I have "American" competition?

>zone offset on a variety of dates to see if it changes. Based on
>that, I developed the following checkDST() function which, as far as I
>can tell, should be sufficient.
>
>It checks either the date passed to it or the current date with 3
>other dates, each 3 months further into the future.


Checking the date in question, and January 1st, and July 1st, should
suffice - unless someone does something silly with Summer Time. ISTR
that Australia fiddled the rules for the Olympics, but probably not
enough to matter.


>Can anyone suggest whether the algorithm is appropriate and if my
>implementation is OK? It will fail if there are changes to time zone
>offsets that are not related to daylight saving, and if changes occur
>for a period of less than 3 months and fall within the dates tested.


ISO/IEC 16262 requires that the CURRENT Summer Time rules are applied
for ALL years. Most OSs only know one set of rules per location, but
AIUI Vista knows up to 2 sets, and changes.

US legislation, IIRC, changed the rules with effect from 2007-03-01
(presumably local time; possibly DC time). Therefore, Randy et al
should have stopped running Javascript on pre-Vista PCs before
2007-02-28 24:00 LCT, should have updated their systems, and not
restarted Javascript before 2007-03-01 00:00 LCT.

I don't know whether Vista does it right.

Some systems, IIRC, using UNIX, get the OS to do date work that
Javascript ought to do; in that case, the wrong (by 16262) rules or zone
may be applied to non-current dates.

I recall no provision for changing time zone at a location, other than
by the Control Panel settings.


-----

I recall no complaints about <URL:http://www.merlyn.demon.co.uk/js-
dates.htm#YS> first half.



NOTE : Summer Time does NOT change the Time Zone; it only changes the
offset from GMT.


You have

function checkDST(d) {
d = d || new Date();
var x = new Date(d);

If d is not supplied, or is 1970-01-01 00:00:00.000 UTC, or is 0, or is
NaN, both new Date() and new Date(d) will be called and the latter would
be better as new Date(+d). If d is supplied as a string, new Date() may
get it wrong. Under-tested.

Less flexibility would be safer? insist on a Date Object, and copy it -
or maybe

function checkST(d) { var U
d = d == U ? new Date() : new Date(+d)

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-21-2007
Randy Webb wrote on 21 dec 2007 in comp.lang.javascript:

>> IE7 and Central European Time Zone tested.

>
> It tells me I have a summertime offset in Northern hemisphere, but, it
> doesn't tell me whether I use Daylight Savings Time or not. No setting
> on the PC can either. Arizona, USA, is split on DST. Parts of the
> state use it and parts don't. Indiana was another state that just
> recently started using it.


Do you mean if you are using summertime offset at present?

That is a simple extension:


<script type='text/javascript'>

var d1 = new Date(2007,0,1).getTimezoneOffset()
var d2 = new Date(2007,6,1).getTimezoneOffset()
var dn = new Date().getTimezoneOffset()

var t = 'This computer is set for a zone with'

if (d1==d2)
alert(t + 'out summertime offset');
else if (d1>d2) {
z = t + ' summertime offset in Northern hemisphere\n';
if (dn==d1)
alert(z + 'At present the offset is off');
else
alert(z + 'At present the offset is on');
};
else
{
z = t + ' summertime offset in Southern hemisphere\n';
if (dn==d2)
alert(z + 'At present the offset is off');
else
alert(z + 'At present the offset is on');
};

</script>

Not tested for Southern hemisphere


>> That such offset saves daylight is absolute nonsense,
>> though it may save energy consumption.

>
> It doesn't "save daylight", it just changes the hours in the day when
> it is there. Personally, I wish they would just do away with the
> entire concept as it is a broken phenomenon now.


Which would be even more terrible for the
'JS Date() cum Windows settings object'
then the crap of the present implementation.
Does it know when this summertime concept started in each zone?
Does the London local setting know about the "double summertime" of WWII?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-21-2007
Randy Webb wrote on 21 dec 2007 in comp.lang.javascript:
> -Lost said the following on 12/20/2007 9:24 PM:
>> Why in the world did Congress or whoever change that at the last moment
>> anyway?


What congress?

Do you mean 'congress' in the sense of the Kamasutra
and changing a stand of such congress at the last moment?

Wouldn't that put you off, Lost?

> <shrug> Why do politicians do any of the things they do?
>
> One reason I read (and don't know if it is true or not) is:
>
> A new law to extend DST to the first Sunday in November will take effect
> in 2007, with the purpose of providing trick-or-treaters more light and
> therefore more safety from traffic accidents.


What law?

Is that what politicians do, Randy, changing a stand at the last moment?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      12-21-2007
In comp.lang.javascript message <(E-Mail Removed)>,
Thu, 20 Dec 2007 14:15:01, Randy Webb <(E-Mail Removed)> posted:
>
>Not for what Rob is wanting to test. The only way to know if it is
>getting it correct in different time zones is to change the time zone
>settings.


But can one be certain, in Windows, that such a temporary change cannot
possibly have a lasting adverse effect, such as but not limited to
entries in calendar/diary software and limited-duration software that
looks for time reversal or performing scheduled activity?

> It is just as trivial to do that for testing as changing the
>time/date. I just didn't do it when I was testing. Right click the
>clock on the taskbar in Windows>Adjust Date and Time>Time Zone tab.
>Change it, apply it, test it, repeat. There is also a setting there for
>automatically changing the time for DST. With it unchecked, the test
>gives incorrect results for me when testing my own time zone.
>
>"undefined: Daylight saving is not observed here at all. "
>
>And that is in Eastern Standard Time Zone in the US.


Well, if DST is turned off one must expect it not to be found.
Obviously great care must be taken with the wording of such messages.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk 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.
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      12-21-2007
Randy Webb wrote on 21 dec 2007 in comp.lang.javascript:

> It should. It has a setting to let Windows change the time for you. By
> law, in the USA, it always starts and ends on the same days every
> year, no matter what zone you are in in the USA, if you use it.


That gives two nights that this offset is rippling though your continent,
and must be terrible for large organisations like radio/tv.

The European law switching at the same time across all timezones is
slightly better.

>> Does the London local setting know about the "double summertime" of
>> WWII?

>
> No idea.


In general, MS-windows zones surely cannot fill in the historical patchwork
of summertime regions.

<http://en.wikipedia.org/wiki/Daylight_saving_time>
points to an European viewpoint:

Guess whose!

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Tom
Guest
Posts: n/a
 
      12-21-2007
On Wed, 19 Dec 2007 22:27:46 -0800 (PST), RobG wrote...
>
>I was investigating a function to determine whether daylight saving
>was being observed on a particular date (given the platform's regional
>settings) and came across a suggestion at merlyn.com to test the time
>zone offset on a variety of dates to see if it changes. Based on
>that, I developed the following checkDST() function which, as far as I
>can tell, should be sufficient.
>
>It checks either the date passed to it or the current date with 3
>other dates, each 3 months further into the future. If any of their
>time zones differs from the epoch then either:
>
> true - daylight saving is being observed on the date, or
>
> false - daylight saving is observed but not on the date
>
>are returned. If no difference is found, undefined is returned. I
>explicitly return undefined as a matter of style rather than let the
>loop peter out and return undefined by default.
>
>Can anyone suggest whether the algorithm is appropriate and if my
>implementation is OK? It will fail if there are changes to time zone
>offsets that are not related to daylight saving, and if changes occur
>for a period of less than 3 months and fall within the dates tested.
>
>As far as I know, there isn't anywhere that does that, but they might
>be accommodated by an "acceptable range" test (say allow changes of up
>to 5 minutes) and checking for symmetry - if the time zone is
>different in x months time, check if it was also different (12 - x)
>months ago, and check if it differed by the same amount taking into
>consideration the acceptable range.
>
>
>function checkDST(d) {
>
> d = d || new Date();
>
> var x = new Date(d);
> var dt = d.getTimezoneOffset();
> var xt;
>
> for (var i=0; i<3; i++) {
> x.setMonth(x.getMonth() + 3)
> xt = x.getTimezoneOffset();
>
> if (dt != xt) {
> return dt < xt;
> }
> }
> return undefined;
>}
>
>function showDST(){
>
> var x = isDST(new Date());
> var msg;
>
> if (x === true) {
> msg = 'Daylight saving is observed here ' +
> 'and is in force.';
> } else if (x === false) {
> msg = 'Daylight saving is observed here, ' +
> 'but not at the moment.';
> } else {
> msg = 'Daylight saving is not observed here at all.'
> }
> alert(x + ': ' + msg);
>}
>
>showDST();
>
>
>


I don't know if you could find something that is reliable and accurate in the
long run. As an example I'm in the Pacific time zone and our government
arbitrarily decided to extended the length of Daylight Savings.

I think you can collect the time zone information
(http://www.w3schools.com/jsref/jsref_obj_date.asp), but I don't think there is
any information there to determine if a timezone used daylight savings or not.

Tom
--
Newsguy 20 GB Free Trial Accounts
http://newsguy.com/overview.htm

 
Reply With Quote
 
The Magpie
Guest
Posts: n/a
 
      12-21-2007
Evertjan. wrote:
> Randy Webb wrote on 20 dec 2007 in comp.lang.javascript:
>
>> Not for what Rob is wanting to test. The only way to know if it is
>> getting it correct in different time zones is to change the time zone
>> settings.[snip]

>

Ok - so you *do* all know this is a waste of time don't you? There is
one and only one way to do it. You get the user to state explicitly
what time zone they are in, then you check the *actual* date and time
in that time zone (not the one on the PC - so you will need a remote
time-server somewhere) and then you apply the rules in force in that
specific time zone.

And anyone who wants to do that in Javascript is completely insane!

You have one - count them - *one* choice only. Find a way to ask the
operating system and presume the operating system has the time zone
details *and* rules set as the user wants them. Then check a remote
time-server. Even then, it may not be correct because, of course, the
user may choose to set his or her PC to - let's say - GMT because he
likes to do amateur astronomy and it makes it easier.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a given local time to UT considering Daylight Saving Times.... Filip Lyncker C++ 3 04-05-2005 02:52 PM
Daylight saving Amitabh Deepak C++ 4 09-17-2004 12:38 PM
Daylight saving 2004-10-31 03:00 EEST/EET? NoName NoName Java 0 04-12-2004 07:21 PM
Daylight Saving time: How to identify if a given time is invalid? Pranav Kantawala Java 5 02-25-2004 06:28 PM
Converting datetimes from/to UTC/GMT including daylight saving Michael Pfeifer C++ 4 12-17-2003 09:18 PM



Advertisments