Interesting ideas.
Subtracting 8 hours does seem like a solution except I will need to take into account adding or subtracting enough MINUTES so that I accurately keep track of crossing midnight. The reason I say minutes is that some places have a local time with a 30 minute difference.
Using Epoch time is interesting and I partially did that in the last couple of hours. However, I was worried that the number of minutes in a year exceeded the size of a WORD variable (LONG variables are part of the PIC 18F series and I am using a 16F).
Later on today and with some more forum searching, I came up with a partial solution.
Working on the premise that a large display clock only displays the hour and the minute, you only need to worry about the last 24 hours. Thus, you do not need to worry about a DAY OF MONTH, MONTH or YEAR change. That greatly shortens the code. I'll tackle those at a later date. Thus, my largest number would be 1440 minutes which will fit in a WORD variable.
Next I needed to convert from BCD to DEC and back. Remember I am using BCD because the RTC stores it that way. I am using DEC to do the epoch calculations. A couple of posts on this forum provided some important clues.
To convert BCD to DEC (I love the simplicity of it!)
myDEC = ((myBCD >> 4) * 10) + (myBCD & $0F)
To convert the DEC to BCD, the forum provided another clue by using the DIG mathematical operator (pg 74 in manual, sec 3.1.9):
myBCD = ((myDEC DIG 1) * 16) + (myDEC DIG 0)
Again, I did not invent these, they were found on the forum.
CONVERTING TO LOCAL TIME:
Remember I only have to do the hours and minutes for now (I have already burned too many brain cells on this project today, I'll tackle the rest at another date)
' ==== First I read the RTC and to bring the BCD values into ClkYear,ClkMon, etc
' ==== Next I convert all those BCD values to DEC so I can do Epoch calculations
DstYear =((ClkYear >> 4) * 10) + (ClkYear & $0F) ' last two digits of year
DstMon = ((ClkMon >> 4) * 10) + (ClkMon & $0F) ' Month starting at 01
DstDte = ((ClkDte >> 4) * 10) + (ClkDte & $0F) ' Date of the month, i.e. the 13th
DstDOW = ClkDOW ' Day of week (I don't use this)
DstHrs = ((ClkHrs >> 4) * 10) + (ClkHrs & $0F) ' 24 hour clock
DstMin = ((ClkMin >> 4) * 10) + (ClkMin & $0F) ' minutes
DstSec = ((ClkSec >> 4) * 10) + (ClkSec & $0F) ' seconds
' ==== Now I make a mini epoch :-) of the last 24 hours
' ==== DstTime contains the number of minutes in minutes, hours and Day of the Month (DstDte)
DstTime = DstMin + (DstHrs * 60) ' number of minutes in minutes and hours
DstTime = DstTime + (DstDte * 1440) ' At this point you have a mini Epoch of the number of minutes in this month
' ==== Next subtract out the number of minutes from GMT.
DstTime = DstTime - DstOffset ' for me in California, this is 7 hours * 60 minutes or 420 minutes
' ==== This won't work for most of the world, I just need to get something working for
' ==== where I live in California U.S.A. I'll work out the rest of the world later.
' ==== Also, this does not automatically change from DST to ST..... later, later, later
' ==== Finally, convert your mini Epoch to decimal times
DstDte = DstTime / 1440 ' Day of the month
DstHrs = (DstTime // 1440) / 60 ' Military Hours
DstMin = (DstTime // 60) ' Minutes
Hope that helps. Now I need to finish my project and then come back to this and make it into a usable set of functions. :-)
Thank you all for the ideas!
To-Do Ideas:
1) Check to see what happens at month crossover. Need to allocate for months with different number of days and Feb with 29 days
2) Once a month changes, check to see if year changed.
3) Need a configuration file that has the name of the timezone, start of DST, end of DST and offset with plus/minus in minutes. Dates will likely be different from year to year (see http://www.timeanddate.com/time/dst/ )
4) Need to store config file. How about in RTC which has 56 bytes of battery backed memory.
5) When year changes, go look for config file and update config
6) Tie all of this into a tutorial on how to interface the ESP8266 wifi module (about $3) so you don't have to use WWVB, GPS, etc.
7) How to determine your approx location??? Does wifi module know physical location of gateway? That way you could set config automagically
8) Is there an easy way to do BCD arithmetic?
=== End of Message ===
Bookmarks