Clean code for DST calculation based on GPS data


+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    913

    Default Clean code for DST calculation based on GPS data

    Hi there,

    I'm quite close to finish my large wall clock project where the time base is a GPS receiver.

    Nothing to worry here, everything works fine.

    I have made a routine to set my DST flag ON or OFF BUT I think it must be feasible a much better way.

    My code is made of all the possible cases encountered during the year and when the first positive case is reached, it leaves all others cases.

    If one of you has a few moments to give it a look and come back with suggestions, I'll be very happy to see them.

    Here some info about the code here under.

    I live in Switzerland (EUROPE) and DST starts this year (2024) on March 31st @ 02:00 and ends on October 27th @ 03:00.

    Here's my piece of code where:
    - DST is a BIT var, DST 1 = summer time, DST 0 = winter time
    - DayOfYear ia WORD var and is a concatenation of month (1..12) and day (1..31) - first DayOfYear = 101, last DayOfYear = 1231
    - DSTStart and DSTEnd are BYTE vars and represent respectively DST ON and OFF days for that year (thanks to Enigma for his amazing formula) which are in 2024 DSTStart = 31 (March) and DSTEnd 27 (October)
    - Hours is a BYTE var holding from 1..23

    Code:
        ' Before DST ON period
        if DayOfYear < dstStart then
            DST = 0
            GOTO LEAVE_DST_PERIOD
        ENDIF
    
        ' First day of DST ON still OFF until 02:00
        if DayOfYear = dstsTART then
            if Hours < 2 THEN
                DST = 0
                GOTO LEAVE_DST_PERIOD
            ENDIF    
        ENDIF 
    
        ' First day of DST ON @ 02:00 and later
        if DayOfYear = dstsTART then
            if Hours => 2 THEN
                DST = 1
                GOTO LEAVE_DST_PERIOD
            ENDIF    
        ENDIF    
    
        ' Days between DSTStart and DSTEnd
        if DayOfYear > dstsTART then
            if DayOfYear < dsteND then
                DST = 1
                GOTO LEAVE_DST_PERIOD
            ENDIF            
        ENDIF
    
        ' Last day of DST ON still ON until 03:00
        if DayOfYear = dstEnd then
            if Hours < 3 THEN
                DST = 1
                GOTO LEAVE_DST_PERIOD
            ENDIF
        ENDIF   
        
        ' Last day of DST ON @ 03:00 and later
        if DayOfYear = dstEnd then
            if Hours => 3 THEN
                DST = 0
                GOTO LEAVE_DST_PERIOD
            ENDIF
        ENDIF   
    
        
        ' AFTER DST ON period
        if DayOfYear > dstEnd then
            DST = 0
            GOTO LEAVE_DST_PERIOD
        ENDIF
    
        LEAVE_DST_PERIOD:
    Roger

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,451


    Did you find this post helpful? Yes | No

    Default Re: Clean code for DST calculation based on GPS data

    see post from enigma
    https://www.picbasic.co.uk/forum/sho...?highlight=dst
    i don't think it gets any neater than that
    Warning I'm not a teacher

  3. #3
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    913


    Did you find this post helpful? Yes | No

    Default Clean code for DST calculation based on GPS data

    Thanks Richard.
    Roger

Similar Threads

  1. help clean up some code
    By longpole001 in forum PBP3
    Replies: 10
    Last Post: - 10th April 2014, 12:00
  2. How can I clean up my code?
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 18th June 2012, 23:34
  3. Replies: 1
    Last Post: - 26th June 2009, 23:33
  4. how to add the calculation of temperature in my source code
    By slimpeng in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 20th February 2008, 16:31
  5. GPS+AIS Nmea Data
    By Squibcakes in forum GPS
    Replies: 0
    Last Post: - 23rd September 2005, 05:31

Members who have read this thread : 7

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