DST

  • Thread starter Dr John Stockton
  • Start date
D

Dr John Stockton

After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.
 
M

McKirahan

Dr John Stockton said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.


Before 2100 ... try 2007!

More Daylight Savings: Energy Boon or Scheduling Snafu?
http://news.nationalgeographic.com/news/2005/07/0728_050728_daylight.html

"clock changes would actually take effect in 2007."

"The bill calls for daylight savings to begin three weeks earlier,
on the second Sunday in March, and to end on the first Sunday
in November, one week later than daylight saving time currently does."
 
R

RobG

Dr said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.

Seems to work OK (1987 and onward), but why make your code so difficult
to decipher?

Given the frequency of change and probable need for it to be modified
for a great variety dates, there is a strong need for code that is
portable and easily maintained. It should also handle date ranges that
span New Year.

The wikipedia daylight saving entry is:

<URL:http://en.wikipedia.org/wiki/Daylight_Saving_Time>

The examples below should help.


/* Changeover to and from US DST occurs at 0200 hrs (2:00 am)
* on the following days:
*
* 1987 to 2006 inclusive:
* 1st Sunday in April to last Sunday in October
*
* 2007 onward (until next change...):
* 2nd Sunday in March to first Sunday in November
*
* Note that there have been many changes to the start days for
* DST - see:
* http://aa.usno.navy.mil/faq/docs/daylight_time.html
* http://www.energy.ca.gov/daylightsaving.html
*/
function DSTdatesUS( yr )
{
if ( yr<1987 ) return; // Outside range

// Work out last possible on/off date
var don = (yr<2007)? 38 : 14;
var dof = (yr<2007)? 31 : 38;

// Create initial on/off date objects
var DSTon = new Date(yr, 2, don, 2);
var DSToff = new Date(yr, 9, dof, 2);

// Adjust to Sunday before last possible date (adjustment may be 0)
DSTon.setDate( DSTon.getDate() - DSTon.getDay() );
DSToff.setDate( DSToff.getDate() - DSToff.getDay() );

return [DSTon, DSToff];
}


/* Australian Eastern Daylight Saving Time (AEDST) [on, off]
*
* Since 2001, the changeover to AEDST occurs at 0200 hrs (2:00 am)
* on the last Sunday in October and finishes at 0200 hrs on the
* last Sunday in March the following year.
*
* Note: Of the states 4 states in the AEST time zone, there are 2
* different ranges for daylight saving and one state that doesn't
* observe it at all. Start and end dates vary frequently.
*
* Legislation is before the NSW govt to extend the end of DST in 2006
* by one week.
*
* yr is the start year.
*/
function DSTdatesAEDST( yr )
{
if ( yr<2001 ) return; // Outside range

// Initially set to last possible on/off dates
var don = 31;
var dof = 31;
// If legislation passes, NSW DST extends by 7 days in 05/06
if (2005 == yr ) dof += 7;

// Create initial on/off date objects
var DSTon = new Date(yr, 9, don, 2);
var DSToff = new Date(yr, 14, dof, 2);

// Adjust to Sunday before last possible date (adjustment may be 0)
DSTon.setDate( DSTon.getDate() - DSTon.getDay() );
DSToff.setDate( DSToff.getDate() - DSToff.getDay() );

return [DSTon, DSToff];
}
 
R

RobG

McKirahan said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.



Before 2100 ... try 2007!

I think that is JRS humour.

John's code handles the 2007 change but assumes that every year evenly
divisible by 4 is a leap year. Since 2100 isn't a leap year even though
2100%4 = 0, from 2100 onward the code reports changes as occurring on
Saturdays, not Sundays.

Likely John thought that DST changeover rules will be modified before
then, so his code will need to be changed anyway - or his heirs may be
imposed upon to account for his shortsightedness. ;-)

[...]
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 7 Oct
2005 00:24:01, seen in McKirahan
Dr John Stockton said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.


Before 2100 ... try 2007!

More Daylight Savings: Energy Boon or Scheduling Snafu?
http://news.nationalgeographic.com/news/2005/07/0728_050728_daylight.html

"clock changes would actually take effect in 2007."

"The bill calls for daylight savings to begin three weeks earlier,
on the second Sunday in March, and to end on the first Sunday
in November, one week later than daylight saving time currently does."

My code gives results suitable for determining the current state of DST
from after the first Sunday in November 2005 until some time in March
2100, assuming no new legislation. The algorithm is derived from one at
webexhibits, verified and adapted.

You need to learn to think, and to test, before you write; the time that
you waste is mostly that of others.

So does National Geographic, since the second Sunday in March is only
three weeks earlier than the first Sunday in April; about half the time.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 7
Oct 2005 06:32:56, seen in RobG
McKirahan said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.


Before 2100 ... try 2007!

I think that is JRS humour.

John's code handles the 2007 change but assumes that every year evenly
divisible by 4 is a leap year. Since 2100 isn't a leap year even though
2100%4 = 0, from 2100 onward the code reports changes as occurring on
Saturdays, not Sundays.

Likely John thought that DST changeover rules will be modified before
then, so his code will need to be changed anyway - or his heirs may be
imposed upon to account for his shortsightedness. ;-)

Your shortsightedness : I wrote, as you quoted, "and before 2100".
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Fri, 7
Oct 2005 06:12:51, seen in RobG
Dr said:
After December and before 2100, the following should determine the start
and finish dates of local US DST in the current year.
CHECK !!! TEST !!!

Y = new Date().getFullYear()
DSTON = /* March */ Y==2006 ? 33 : T = 14 - (((5*Y/4)|0) + 1) % 7
DSTOFF = /* November */ Y==2006 ? -2 : T - 7
X = [new Date(Y, 2, DSTON, 2), new Date(Y, 10, DSTOFF, 2)]

There may be an error within an hour of the critical moment.

Seems to work OK (1987 and onward), but why make your code so difficult
to decipher?

No, it does not work, and does not claim to work, for years before 2006.
For 1987-2006 :-
Begin April (07 - (5*y div 4 + 4) mod 7) at 02:00 local
End October (31 - (5*y div 4 + 1) mod 7) at 02:00 local

The point is that the day-of-month of the change, given constant rules
in 1900-2099, can easily be calculated by simple arithmetic (for
everywhere with the usual sort of rules); and that for US 2007+ DST, the
relationship between ON & OFF is particularly simple.

In 1900-2099, the javascript day-of-week of Christmas Day is just
Math.floor(5*y/4) % 7

Given the frequency of change and probable need for it to be modified
for a great variety dates, there is a strong need for code that is
portable and easily maintained. It should also handle date ranges that
span New Year.

Mine handles years correctly too; in AU/NZ, the rules for the dates in
the first and second halves of the year are given by similar rules, and
a simple XOR hemisphere will get the direction of the change right.

The wikipedia daylight saving entry is:

<URL:http://en.wikipedia.org/wiki/Daylight_Saving_Time>

Wiki is quite often right, or mostly right.

The examples below should help.

They rely on the javascript date object to determine the date; the
method I gave does not, and could be readily translated into other
languages, at least for after 2006, and could even be learned for doing
in one's head.

// If legislation passes, NSW DST extends by 7 days in 05/06
if (2005 == yr ) dof += 7;

That appears odd - extension only in 2005/06, or permanently from then?
The OFF date for 2005/06 is surely when 2006==yr ?


Has Canada or Mexico legislated to follow the USA?
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top