Which if written in a more human-readable form would appear as 2014-10-31 00:30:00 (or October 31, 2014 at 12:30 AM)
The rules for U.S. Daylight Saving time are "Daylight saving time starts on the second Sunday in March and ends on the first Sunday in November, with the time changes taking place at 2:00 a.m. local time." (Source: Wikipedia). This means that:
If the month is January, February, or December, it is standard time all month.
If the month is April through October it is daylight time all month.
If it is November 8-30 or March 1-7 it is standard time.
If it is March 15-31 it is daylight time.
That part is easy to program. The problem is that the switch occurs on Sunday during the week of March 8-14 in the spring, and on Sunday during the week of November 1-7. But, how do I determine which day is Sunday and adjust accordingly?
Strangely, in the program I am trying to modify the day of the week is available in the variable DW, but it seems the value returned is 0-6 with 0=Tuesday, 1=Wednesday, 2=Thursday, 3=Friday, 4=Saturday, 5=Sunday, and 6=Monday. It appears they are taking a Julian date and doing a MOD 7 on it so as it happens the days of the week don't begin with Sunday.
I thought about creating a function but got stuck here (note that since this is not finished it probably has a few unsquashed bugs, and it has to be compiled with the -lang qb switch):
Code: Select all
DECLARE FUNCTION IsUSDST (DateTime AS STRING, DayWeek AS INTEGER)
FUNCTION IsUSDST (DateTime AS STRING, DayWeek AS INTEGER)
REM Variables used in function: M=Month (1-12), D-Day (1-31), H=Hour (00-23)
IF (M>3) AND (M<11) THEN
ELSEIF (M<3) or (M=12) THEN
IF (M=3) AND (D>14) THEN
ELSEIF ((M=3) AND (D<8)) OR ((M=11) AND (D>7)) THEN
IF M=3 THEN
ELSEIF M=11 THEN
Basically I am stuck on what to put where the ... appears. In the first instance the date will be March 8-14 but if it is BEFORE Sunday I need to return a value of "0" for IsUSDST$, and "1" if it is after. If it is Sunday, then I need to return the value depending on the hour, which would be defined as H=VAL(MID$(DateTime,9,2)) and if H is 0 or 1 then it should return "0", if >2 then "1" and if "2" then "-1" since technically that hour is lost.
In the second instance the date will be November 1-7 but if it is BEFORE Sunday I need to return a value of "1" for IsUSDST$, and "0" if it is after. If it is Sunday, then I need to return the value depending on the hour, which would be defined as in the previous paragraph and if H is 0 or 1 then it should return "1", and if >1 then "0" (in this situation the extra hour doesn't matter since nothing of importance happens during that hour).
It's that whole "determining whether the date is before or after Sunday" that I can't seem to wrap my brain around. Am I making this more complicated than it needs to be? It would be easier if I could just give it a Julian date (which is available in the program as a long integer) and hour, and have it determine if that is or isn't under Daylight Saving Time, but I would have no clue at all how to do that.