By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

DST

P: n/a
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
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.
Oct 6 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:3M**************@merlyn.demon.co.uk...
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/n..._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."
Oct 7 '05 #2

P: n/a
Dr John Stockton wrote:
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];
}
--
Rob
Oct 7 '05 #3

P: n/a
McKirahan wrote:
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:3M**************@merlyn.demon.co.uk...
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. ;-)

[...]
--
Rob
Oct 7 '05 #4

P: n/a
JRS: In article <Mv********************@comcast.com>, dated Fri, 7 Oct
2005 00:24:01, seen in news:comp.lang.javascript, McKirahan
<Ne**@McKirahan.com> posted :
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:3M**************@merlyn.demon.co.uk...
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/n..._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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
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.
Oct 7 '05 #5

P: n/a
JRS: In article <sE*****************@news.optus.net.au>, dated Fri, 7
Oct 2005 06:32:56, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
McKirahan wrote:
"Dr John Stockton" <jr*@merlyn.demon.co.uk> wrote in message
news:3M**************@merlyn.demon.co.uk...
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".

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 7 '05 #6

P: n/a
JRS: In article <Dl*****************@news.optus.net.au>, dated Fri, 7
Oct 2005 06:12:51, seen in news:comp.lang.javascript, RobG
<rg***@iinet.net.au> posted :
Dr John Stockton wrote:
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?

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
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.
Oct 7 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.