R
RobG
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();
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();