Calculating Daylight Saving Time


Closed Thread
Results 1 to 20 of 20

Hybrid View

  1. #1
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427

    Default Calculating Daylight Saving Time

    Hey group,

    Been working on code to universally determine if Daylight Saving Time is or is not in effect.
    Would love a second set of eyes to make sure I have not missed anything or if there is a cleaner (less code) way of doing it.

    I have used the following rules as a basis...
    At present, daylight saving time in the United States
    begins at 2:00 a.m. on the second Sunday of March and
    ends at 2:00 a.m. on the first Sunday of November
    below is a shot of a spread sheet that I made to try and account for all possibilities for when a given month (March and November) might begin.
    Note 1:the seven colums across represent the seven possibilities for what day of the week the month may begin on.
    Note 2: the numbers next to the letter for the day of the week represent the numberd day of the week (ie sun=1, mon=2... sat=7)

    Name:  dst_xel.jpg
Views: 756
Size:  199.7 KB

    Here is my code...
    Code:
    '===========================================
    '---  DST Calc (dec1 dow,dec2 mth,dec2 date)  ---
    dstcalc:
    dst = 1      'covers Apr, May, Jun, Jul, Aug, Sep, Oct
    If (mth=1)  then dst = 0  'Jan
    If (mth=2)  then dst = 0  'Feb
    If (mth=12) then dst = 0  'Dec
    
    March:
    if mth=3 then                                       'March
        if  date <8 then
            dst=0
            elseif (date>7 and date<15) and dow=1 and hours<2 then
            dst=0 'Mar
            elseif (date=8  and dow>1) then
            dst=0
            elseif (date=9  and dow>2) then
            dst=0
            elseif (date=10 and dow>3) then
            dst=0
            elseif (date=11 and dow>4) then
            dst=0
            elseif (date=12 and dow>5) then
            dst=0
            elseif (date=13 and dow>6) then
            dst=0
        endif
    endif
    
    November:
    if  (mth=11) then                                  'November                                      
        if  date >7 then
            dst=0
            elseif (date <8 and dow=1) and hours>1  then
            dst=0               
            elseif (date=2 and dow<3) then
            dst=0
            elseif (date=3 and dow<4) then
            dst=0
            elseif (date=4 and dow<5) then
            dst=0
            elseif (date=5 and dow<6) then
            dst=0
            elseif (date=6 and dow<7) then
            dst=0
        endif
    endif
      
    return
    Note that the code assumes that DST is in effect (dst=1) and tries to find reasons that dst should be set to zero (dst=0)

    your thoughts or ways to improve or simplify??

    thanks
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  2. #2
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    I don't understand your tables... Column c,d,e are years? If there are, what years?
    I was going to post my code for calculating DST, but in my country winter time is from last Sunday in October to last Sunday in March.
    Last Sundays are on same date every 28 years, in fact all days in week are on same day in month every 28 years.
    So just build 2 lookup table for 28years. And then use just Year//28 to search for date in that year.
    If you look hard you can find link between day in March and day in October.
    Here is code for that:
    Code:
      
        Tmp=Year 'Use less code space than Year//28 in lookup table
        WHILE Tmp>27
            Tmp=Tmp-28
        WEND
        LOOKUP Tmp,[Dates in march for 28 years],Tmp'March
        IF Month=10 THEN  'October
            Tmp=Tmp-4
            IF Tmp<25 THEN Tmp=Tmp+7
        ENDIF
    It worked for me, hope that this help.
    And using DST flag can complicate life, so I just move hours up or down.

  3. #3
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    I don't understand your tables... Column c,d,e are years? If there are, what years?
    The seven columns represent ANY year. Since in any given year, the first day of March or November could be a Sun, Mon, Tue, etc. I had to account for that fact that there are 7 POSSIBLE starting days for each month.

    The reason I also show Sun=1, Mon=2, etc., is so I could ask "less than" or "greater than" questions.

    I wanted this to be a universal calculation for USA.

    The code will only apply for these rules...
    At present, daylight saving time in the United States
    begins at 2:00 a.m. on the second Sunday of March and
    ends at 2:00 a.m. on the first Sunday of November
    If you are in another part of the world where you have different rules (or no DST at all) then obviously this would not apply.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

  4. #4
    Join Date
    Feb 2013
    Location
    Quebec, Canada
    Posts
    67


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    Heckler don't you get your time sync from NTP? Do you have the day of week?

    If so:

    Code:
    Delta = Date - 7
    
    IF Month=3 AND Day=Sunday AND (Delta>0 OR Delta<8) Then DST!
    Name:  CalculsDST.png
Views: 672
Size:  26.1 KB

    By the way thank you for the ESPBasic code! Got some ideas...

  5. #5
    Join Date
    Jul 2003
    Location
    Lancashire
    Posts
    50


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    Hi All
    I've used this for many years, works for Europe and I suppose could be adapted for USA. DST Europe changes in March or October, last weekend of the month. The routine calculates the date in the respective month that the change occurs on.
    DSTStart holds the start date for DST
    DSTEnd holds the end date for DST
    Year holds the year in this format e.g 2016

    DSTCheck:
    DSTStart = (31 - (((5*Year)/4)+ 4) mod 7)
    DSTEnd = (31 - (((5*Year/4)) + 1) mod 7)
    Return

    That's it, Pete

  6. #6
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    Can you explain how did you get this formulas?
    I was searching whole net few years ago, and best I could think is to type dates for whole century find pattern, and put it in lookup table.
    EDIT:
    Heckler,
    Just found this
    http://delphiforfun.org/programs/mat...cs/dstcalc.htm
    Basically what Enigma wrote, but it have calculation for US.
    Last edited by pedja089; - 11th March 2016 at 21:36.

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: Calculating Daylight Saving Time

    thanks enigma

    that's very neat and tidy
    for australia
    #au DSTEnd 1st sunday in april = (7- (((5*Year/4)) + 4) % 7)
    #au DSTStart 1st sunday in october = (7- (((5*Year/4)) + 5) % 7)

Similar Threads

  1. Calculating time delay between two inputs?
    By grimmjow in forum General
    Replies: 11
    Last Post: - 18th October 2010, 14:52
  2. Calculating Time for clock's preset
    By menta in forum General
    Replies: 33
    Last Post: - 5th July 2008, 06:09
  3. Calculating elapsed time, how?
    By Eng4444 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 1st June 2006, 09:00
  4. FYI Daylight Saving Time in the U.S.
    By ccsparky in forum General
    Replies: 1
    Last Post: - 7th November 2005, 15:05
  5. Sinus calculating !
    By Don Mario in forum mel PIC BASIC Pro
    Replies: 29
    Last Post: - 28th November 2004, 23:56

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts