GMT to Local Time Routine


Closed Thread
Results 1 to 15 of 15

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Quote Originally Posted by johncouture View Post
    ... I am downloading the GMT network time using an ESP8266 ...
    I'm interested. I do you do it? Is-it possible to decode Unix time without using LONG variables?

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    The string comes from the ESP8266 as:

    107 15 Jul 2015 18:57:01 GMT

    The 107 is just a marker indicating that this is a fresh time sync.

    Using arrayread I put everything into byte variables:

    arrayread buffer,[
    dec3 intAction,skip 1,HEX2 GMTDte,skip 1,STR GMTMonth\3,skip 3,
    hex2 GMTYear,skip 1,HEX2 GMTHrs,skip 1,HEX2 GMTMin,skip 1,
    HEX2 GMTSec,skip 1,str GMTTZ\3]

    Then I convert the JUL into a 7 using a FOR loop.

    Finally I call ClockGetLocal which takes the GMT variables and converts them into
    CLK variables. I use word variables for the offset because it needs to handle a
    number up to 1440 (24hrs * 60 minutes). For my "mini-epoch" I again use a
    word variable (60 * 24 * 31 = 44640)

    Now this routine will work for today until the end of the century. I'm 60+ so
    someone else can solve any time issues after that! :-)

    Remember I was just trying to display a clock of HH:MM. In the process I did
    solve the year, month, day of month, hours and minutes. What remains is a
    way to store the time zone ABBV, the offset +/- and dates when they start and
    stop. That will come later when I incorporate the 256K EEPROM.

    Anyway, the secret to it all is that you do not have to put the years and months
    into an epoch. Just solve for the number of minutes since the beginning of the
    month (max size is 44640 and able to fit into a word variable).

    To make a long story short, determine if you went beyond the beginning of the
    month and then make adjustments to the date, the month and the year if
    necessary.

    I tested it with my time zone (- 8 hours) and it works. Again, still to be worked
    out is a way to convert a time zone string into an offset and be able to handle
    plus or minus minutes from GMT.


    '======================================
    ClockGetLocal:
    ' convert GMT variables to Clk variables

    ' =================
    ' FOR TESTING, put GMT past midnight so we can check day calculation
    ' GMTHrs = $01 ' this will cause it to go to previous day
    ' GMTDte = $01 ' 1st of current month so when offset subtracts, it goes to previous month
    ' GMTMon = $03 ' March
    ' GMTYear = $12

    ' first copy GMT into Clk variables that don't change for now
    ClkYear = GMTYear ' for now we ignore changes
    ClkMon = GMTMon ' for now we ignore changes
    ClkSec = GMTSec ' daylight saving time does not affect seconde
    ClkDOW = GMTDOW ' for now we ignore changes

    ' These are hard coded for now
    ClkTemp = 0
    ClkOffset = 420
    ClkTz = "P"
    ClkTZ1 = "D"
    ClkTz2 = "T"
    ClkTz3 = 0
    ClkTz4 = 0

    ' now convert CLK variables to decimal
    ClkYear =((GMTYear >> 4) * 10) + (GMTYear & $0F)
    ClkMon = ((GMTMon >> 4) * 10) + (GMTMon & $0F)
    ClkDte = ((GMTDte >> 4) * 10) + (GMTDte & $0F)
    ClkHrs = ((GMTHrs >> 4) * 10) + (GMTHrs & $0F)
    ClkMin = ((GMTMin >> 4) * 10) + (GMTMin & $0F)

    ' build a mini epoch amount (days, hours and minutes only)
    ClkTemp = 0
    'time_t = time_t + (CLKYear * (minutes at 0 of cur year)
    'time_t = time_t + (CLKMon * (minutes at 0 of cur month)
    ClkTemp = ClkTemp + (ClkDte * 1440) ' add in days (max 46080) (-19455)
    ClkTemp = ClkTemp + ClkMin + (ClkHrs * 60) ' add in hours and minutes

    ' now subtract minutes offset
    ' for now hard code in -7 huors
    'if GMTOffset < 1500 then
    ' time_t = time_t + GMTOffset
    'else
    ' 2s compliment?
    ClkTemp = ClkTemp - ClkOffset
    'endif

    ' adjust for year, month, day

    ' first, is this a leap year?
    if (ClkYear // 4) == 0 then
    ClkDays[2]=29 ' yes, adjust Feb # days
    endif

    ' How many days in mini epoch?
    ClkDte = (ClkTemp / 1440)

    ' After we subtracted the offset
    ' How many days were left in the mini-epoch?
    ' Zero means you crossed midnight
    if ClkDte = 0 then
    ' need to figure out last day of previous month
    ClkMon = ClkMon - 1
    ' if zero, you crossed new year
    if ClkMon = 0 then
    ' you won't have to worry about century
    ClkYear = ClkYear -1
    ClkMon = 12
    endif
    ' Feb was already accounted for above
    ClkDte = ClkDays[ClkMon]
    endif

    ClkHrs = (ClkTemp // 1440) / 60
    ClkMin = ClkTemp // 60

    ' now convert back to BCD
    GMTTemp = ((ClkYear / 10) * 16) + (ClkYear // 10)
    ClkYear = GMTTemp
    GMTTemp = ((ClkMon / 10) * 16) + (ClkMon // 10)
    ClkMon = GMTTemp
    GMTTemp = ((ClkDte / 10) * 16) + (ClkDte // 10)
    ClkDte = GMTTemp
    GMTTemp = ((ClkHrs / 10) * 16) + (ClkHrs // 10)
    ClkHrs = GMTTemp
    GMTTemp = ((ClkMin / 10) * 16) + (ClkMin // 10)
    ClkMin = GMTTemp
    return



    '======================================
    ' CLOCK VARIABLES
    ' Updated:
    ' 08/08/2015 21:24 - JJC clock date/time stored in GMT
    ' Had to adjust for local time
    ' http://www.timeanddate.com/time/dst/
    ' 03/16/2014 15:21 - JJC increased length to 30
    ' 12/18/2012 18:03 - JJC added display mode
    ' 12/16/2012 14:13 - JJC added relay status
    '
    '======================================
    Clock VAR Byte[32] 'Clock Array for clock routines
    ClkCtl var Clock[0] 'Clock Control
    ClkHSec var Clock[1] 'Clock Hundredths Seconds
    ClkSec VAR clock[2] 'Clock Seconds GMT
    ClkMin VAR clock[3] 'Clock Minutes GMT
    ClkHrs VAR clock[4] 'Clock Hours GMT
    ClkDte VAR clock[5] 'Clock Date GMT
    ClkMon VAR clock[6] 'Clock Month GMT
    ClkTmr VAR clock[7] 'Timer Control
    ClkAlm VAR clock[8] 'Alarm Control
    ClkAlmHSec VAR clock[9] 'Alarm Hundredths Seconds
    ClkAlmSec VAR clock[10] 'Alarm Seconds
    ClkAlmMin VAR clock[11] 'Alarm Minutes
    ClkAlmHrs VAR clock[12] 'Alarm Hours
    ClkAlmDte VAR clock[13] 'Alarm Date
    ClkAlmMon VAR clock[14] 'Alarm Month
    ClkAlmYear VAR clock[15] 'Alarm Year
    ClkYear VAR Clock[16] 'Clock Year (last 2 digits) GMT
    ClkDOW var Clock[17] 'Clock Day of the Week GMT
    ClkRelay var Clock[18] 'Relay Status Byte
    ClkMode var Clock[19] 'Clock Display Mode
    ClkTempC var Clock[20] 'Temp in C
    ClkTempF var Clock[21] 'Temp in F
    ClkSavMin var Clock[22] 'Last Transmission Min
    ClkSavHrs var Clock[23] 'Last Transmission Hrs
    ClkSavDte var Clock[24] 'Last Transmission Dte
    ClkTZ var Clock[27]
    ClkTZ1 var Clock[28]
    ClkTZ2 var Clock[29]
    ClkTZ3 var Clock[30]
    ClkTZ4 var Clock[31]

    ClkMonth var byte[3] 'Three character Month like Apr,Jun,Dec
    ClkDays var byte[13] 'Number of days in a month
    Arraywrite ClkDays, [0,31,28,31,30,31,30,31,31,30,31,30,31]
    ClkMonths var byte[52] 'Array of three character months
    arraywrite ClkMonths,[" Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec "]
    ClkOffset var word ' number of minutes from GMT
    ClkTemp var word 'Temporary throwaway value for mini epoch

    GMTClock var byte[32]
    GMTCtl var GMTClock[00] 'Clock Control
    GMTHSec var GMTClock[01] 'Clock Hundredths Seconds
    GMTSec VAR GMTClock[02] 'Clock Seconds GMT
    GMTMin VAR GMTClock[03] 'Clock Minutes GMT
    GMTHrs VAR GMTClock[04] 'Clock Hours GMT
    GMTDte VAR GMTClock[05] 'Clock Date GMT
    GMTMon VAR GMTClock[06] 'Clock Month GMT
    GMTYear var GMTClock[07] 'Clock Year
    GMTDOW var GMTClock[08] 'Clock Day of Week
    GMTTemp var GMTClock[09] ' just needed a calculation byte
    GMTMonth var GMTClock[24] ' leave three bytes
    GMTTZ var GMTClock[27] ' leave five bytes
    GMTTZ1 var GMTClock[28] ' leave five bytes
    GMTTZ2 var GMTClock[29] ' leave five bytes
    GMTTZ3 var GMTClock[30] ' leave five bytes
    GMTTZ4 var GMTClock[31] ' leave five bytes

    ' Other items that need to be stored in EEPROM
    'i2cwrite SDA,SCL,RTC,$00 ' TimeZone ID (5 letters), DST (Y/N),
    ' Offset (leading +/- 1440 minutes)
    'intIPAddr = ""
    'intPortAddr = ""
    'intIPGateway = ""
    'intIPMask = ""
    'intDNS1 = ""
    'intDNS2 = ""
    'strSSID = "router ssid"
    'strPW = "router password"
    'strPVKey = "thingspeak key"
    'strTime = "13 Jul 2015 00:00:00 GMT"
    'your location (city state)
    'your Lat/Long/Elevation
    'zip code to location routine?

    === END OF MESSAGE ===

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


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Great! So you are using http://www.timeanddate.com as your time source? At home a setup a HTML page on my Windows Home Server with minimal headers giving me the current datetime but it's not available outside my network. For my lawnmower project I use my cellphone internet sharing because my router is in the basement and I had connections problems. This prevents me from getting the time to my server. That's why is was asking.


    Code:
    <%
    response.write("*" & Format(now, "yyyy-MM-dd HH:mm:ss") & "J")
    
    select case Format(now, "ddd")
    	case "Sun"
    		response.write("1")
    	case "Mon"
    		response.write("2")
    	case "Tue"
    		response.write("3")
    	case "Wed"
    		response.write("4")
    	case "Thu"
    		response.write("5")
    	case "Fri"
    		response.write("6")
    	case "Sat"
    		response.write("7")
    	case else
    		response.write("0")
    end select
    response.write("*" & VbCrLf)
    
    %>
    and the output looks like this:

    Code:
    *2015-08-19 22:33:18J4*
    It's easy to parse and I have DST, date and day number but it's limited to my local network... till I find a free ASP hosting...

    Thanks for sharing!

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    No, The string comes from the ESP8266 as:

    107 15 Jul 2015 18:57:01 GMT

    The ESP8266 gets it from Google.com

Similar Threads

  1. local exchanges
    By datacomms in forum Ethernet
    Replies: 1
    Last Post: - 28th March 2022, 23:21
  2. Simple "Time-out" routine
    By JacoMuller in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 4th May 2010, 10:16
  3. Internet-Ethernet and Local Net
    By Ioannis in forum General
    Replies: 15
    Last Post: - 7th January 2008, 22:16
  4. local variables
    By BigWumpus in forum PBP Wish List
    Replies: 2
    Last Post: - 11th April 2006, 00:39
  5. RTC 1302 !!! Set time Routine !!
    By uludere72 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th February 2006, 10:28

Members who have read this thread : 0

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

Tags for this Thread

Posting Permissions

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