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 ===




Bookmarks