Shop OBEX P1 Docs P2 Docs Learn Events
Daylight saving time calculation in SPIN ? — Parallax Forums

Daylight saving time calculation in SPIN ?

Does anyone have a spin implementation of US daylight saving time calculation? I'm looking for something that given a year, month, and day will tell me whether DST is in effect. I suppose if need be, I could just include a static table of known DST dates, but I was hoping there's something cleverer.

Thanks,
Scott

Comments

  • In Spin, DST begins on this date in March:
    MarDate := 14 - (1 + year * 5 / 4) // 7
    

    It ends on this date in November:
    NovDate := 7 - (1 + year * 5 / 4) // 7
    

    So, in 2017, DST began on the 14 - (1 + 2017 * 5 / 4) // 7 == 12th of March.
    It ends on the 7 - (1 + 2017 * 5 / 4) // 7 == 5th of November.

    Here's where I got it: http://ask.metafilter.com/107559/Magical-DST-Formula-or-just-funky-math

    The rest of your calculations should be easy from there.

    -Phil
  • RaymanRayman Posts: 14,646
    Here's some Spin code I use for this:
    PRI GetTZ(pTime):tz|i,standard,year,mday,hour,mon,bSaving
      'returns local time offset from GMT (in seconds) (based on local time in time structure @Time1)
      'Sets the tzname string based on whether or not DST is in affect
      'In USA with 1 hour DST:  Note that one local hour in Spring doesn't exist and one hour in Fall comes twice... This function presumes the Fall hour is not DST! 
      'Here's some relevant rules for North America (from txdata2001i.tar.gz)  'You Europeans are on your own! :)
        '# Rule  NAME    FROM    TO      TYPE    IN      ON      AT      SAVE    LETTER/S
        'Rule    US      1967    1973    -       Apr     lastSun 2:00    1:00    D
        'Rule    US      1974    only    -       Jan     6       2:00    1:00    D
        'Rule    US      1975    only    -       Feb     23      2:00    1:00    D
        'Rule    US      1976    1986    -       Apr     lastSun 2:00    1:00    D
        'Rule    US      1987    2006    -       Apr     Sun>=1  2:00    1:00    D
        'Rule    US      2007    max     -       Mar     Sun>=8  2:00    1:00    D
        'Rule    US      2007    max     -       Nov     Sun>=1  2:00    0       S
         
        '# Zone  NAME            GMTOFF  RULES   FORMAT  [UNTIL]
        'Zone    EST5EDT          -5:00  US      E%sT
        'Zone    CST6CDT          -6:00  US      C%sT
        'Zone    MST7MDT          -7:00  US      M%sT
        'Zone    PST8PDT          -8:00  US      P%sT
    
        bSaving:=false
        'using formula from here:  http://www.skipgeel.com/dst.html
        year:=word[pTime][5]+1900
        mday:= word[pTime][3]
        hour:= word[pTime][2]
        mon:=word[pTime][4]    
        'Appears to be valid from 1987
        if (year=>2007) 
          case (mon)
            2:  'DST begins in March on second Sunday at 2 AM
              i:=14-(1+year*5/4)//7
              if (mday>i) OR ((mday==i) AND (hour>1))
                bSaving:=true
            3..9:  'April to October is DST
              bSaving:=true
            10:  'DST ends in November on first sunday at 2 AM
              i:=7-(1+year*5/4)//7
              if NOT ((mday>i) OR ((mday==i) AND (hour>1)))
                bSaving:=true
        else
          case (mon)
            3:  'DST begins in April
              i:=1+(2+6*year-year/4)//7
              if (mday>i) OR ((mday==i) AND (hour>1))
                bSaving:=true
            3..8:  'April to September is DST
              bSaving:=true
            9:  'DST ends in October
              i:=31-(year*5/4)//7
              if NOT((mday>i) OR ((mday==i) AND (hour>1)))
                bSaving:=true
    
  • RaymanRayman Posts: 14,646
    Looks like the same formula Phil found...
  • Thanks, working perfectly.

    I can now close out that ticket the wife submitted about incorrect clocks!

    Scott
Sign In or Register to comment.