GMT to Local Time Routine


Closed Thread
Results 1 to 15 of 15

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

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

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Oh, a couple of side notes:

    1) technically you do not need to worry about seconds because GMT to local time share the same seconds.

    2) if you want to ignore those places that do partial hours, you can eliminate minutes too. But be nice to India, Australia, Newfoundland, etc. :-)

  3. #3
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    In Eastern Australia we are +10.

    You are lucky if you are only displaying 24 hour time, and not the date,
    or you need a full calendar all the way to checking leap years.
    Just the one hour ahead the hour before midnight on a New Year’s Eve,
    would make the entire date and day of week incorrect otherwise.

    The calendar code I have posted here has done fine on that with my testing so far.
    It is also the thing that tells you if any of it’s fields changed.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Thank you Art! I'll look at it.

  5. #5
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Quote Originally Posted by Art View Post
    In Eastern Australia we are +10.

    You are lucky if you are only displaying 24 hour time, and not the date,
    or you need a full calendar all the way to checking leap years.
    Just the one hour ahead the hour before midnight on a New Year’s Eve,
    would make the entire date and day of week incorrect otherwise.
    Thats why its better working with EpochTime.
    Thanks and Regards;
    Gadelhas

  6. #6
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    How do you not still need a calendar?.. You can calculate all you want from an epoch moment,
    how do you then know the day of week, or if a date such as the 45th of June 1955 is invalid?
    The calendar still counts from an epoch... you need to begin with a known leap year.

  7. #7
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    Quote Originally Posted by Art View Post
    How do you not still need a calendar?.. You can calculate all you want from an epoch moment,
    how do you then know the day of week, or if a date such as the 45th of June 1955 is invalid?
    The calendar still counts from an epoch... you need to begin with a known leap year.
    Hi Art;

    The Epoch/Unix time ( 32 bits ) its valid since 1970 to 1938. Probably it will be changed to 64bits and then it will be valid longer than the age of the universe. ( Probably )

    Several Industrial systems and Computer systems uses this aproach.
    You don't need a calender for nothing, you can calculate everything from epoch time, including day of week, and also if a some date is valid or not.
    By the way , to calculate day of week, or leap year you don't even need epoch time.

    References;

    Epoch/Unix time - https://en.wikipedia.org/wiki/Unix_time
    Day of week calculation - https://en.wikipedia.org/wiki/Determ...ay_of_the_week

    Calculating leap year. ( the algorith that i use ) You can find others;
    if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
    {
    isleapyear;
    }
    else
    {
    isnotleapyear;
    }

    Videos;
    Thanks and Regards;
    Gadelhas

  8. #8
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: GMT to Local Time Routine

    This sounds like exactly what I did.. an maybe just that I call the code a calendar.
    Except that I have a lookup table of leap years and start the algorithm from the closest to current.
    Actually, thinking again, it might have been dates known to be a Monday on the first day.

Similar Threads

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