Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : GMT date etc from local time
Author | Message | ||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
I had a program that depended on GMT, and so to derive it I subtracted the time difference from local time from a RTC. But I forgot to ripple through the possible date/month/year changes that might be needed. So this is hopefully a trivial snip that does that. Does it look OK? Clunky as, but clunky is easier to revisit ( Not worrying about daylight saving ) CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC C PROGRAM gmtfromrtc C C Reads RTC and calculates GMT C C Input : C RTC time, offset C C Time zone offset is Hours ahead of GMT C eg Sydney is 10.0. C C Output : GMT C CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC INTEGER*4 day, mnth, year, hournt, min, sec INTEGER*4 mingmt, hrgmt, daygmt, mongmt, yeargmt INTEGER*1 zone, timevec(6), mnleng(12), yrem C C Setup C C Safe defaults C day = 1 mnth = 12 year = 2015 min = 0 sec = 0 hournt = 0 zone = 10 C month length mnleng(1) = 31 mnleng(2) = 28 mnleng(3) = 31 mnleng(4) = 30 mnleng(5) = 31 mnleng(6) = 30 mnleng(7) = 31 mnleng(8) = 31 mnleng(9) = 30 mnleng(10) = 31 mnleng(11) = 30 mnleng(12) = 31 SYSRTC timevec C local time from rtc day = timevec(1) mnth = timevec(2) year = timevec(3) + 2000 hournt = timevec(4) min = timevec(5) sec = timevec(6) C set february length for leap years 2001-2099 yrem = year - ( year / 4 ) * 4 IF ( yrem = 0 ) mnleng(2) = 29 C initial values mingmt = min hrgmt = hournt daygmt = day mongmt = mnth yeargmt = year C Adjust hour and so day, month, year as necessary IF ( zone > 0 ) THEN hrgmt = hrgmt - zone IF ( hrgmt < 0 ) THEN hrgmt = hrgmt + 24 daygmt = daygmt - 1 END IF IF ( daygmt < 1 ) THEN mongmt = mongmt - 1 IF (mongmt < 1) THEN mongmt = 12 yeargmt = yeargmt - 1 END IF daygmt = mnleng(mongmt) END IF END IF IF ( zone < 0 ) THEN hrgmt = hrgmt + zone IF ( hrgmt > 24 ) THEN hrgmt = hrgmt - 24 daygmt = daygmt + 1 END IF IF ( daygmt > mnleng(mongmt) ) THEN daygmt = 1 mongmt = mongmt + 1 IF (mongmt > 12) THEN mongmt = 1 yeargmt = yeargmt + 1 END IF END IF END IF PRINT *, "Time zone offset " , zone PRINT *, "Day, month, year" PRINT *, "Hour, min, sec" PRINT *, PRINT *, "Local time :" PRINT *, day," ", mnth," ",year PRINT *, hournt," ",min," ",sec PRINT *, "GMT is :" PRINT *, daygmt," ",mongmt," ",yeargmt PRINT *, hrgmt," ",mingmt," ",sec PRINT *, "Finish" END with output : Time zone offset 10 Day, month, year Hour, min, sec Local time : 28 6 2024 0 35 0 GMT is : 27 6 2024 14 35 0 Finish Edited 2024-06-28 01:23 by zeitfest |
||||
Mixtel90 Guru Joined: 05/10/2019 Location: United KingdomPosts: 6783 |
Would it be easier to keep the RTC at UTC / GMT and then read TIME$ into LTIM$, modified for local time? There is a routine in FotS to apply daylight savings time automatically once you have got local time sorted out. Mick Zilog Inside! nascom.info for Nascom & Gemini Preliminary MMBasic docs & my PCB designs |
||||
matherp Guru Joined: 11/12/2012 Location: United KingdomPosts: 9110 |
Much easier using the epoch and datetime$ functions a%=epoch(now) a%=a%-10*60*60 b$=datetime$(a%) Edited 2024-06-28 02:53 by matherp |
||||
CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2075 |
... and in case you change your mind and do want to do DST, you need to know when to add it on... (easy tweak for your time zone) IsDST() run your clock in base time, then just adjust it when you want to display it. Edited 2024-06-28 03:53 by CaptainBoing |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
Initially I was setting the RTC to GMT, but the reverse complication was needed when the clock had to be set / reset (which is too often during development for various reasons ) and the local time display is useful. It has to be able to be reset by users with local time. (DST will be extra) It isn't MMBasic. Maybe I should add the epoch, datetime functions ... they won't be used much otherwise though. |
||||
zeitfest Guru Joined: 31/07/2019 Location: AustraliaPosts: 482 |
Digging deeper-- Pre-C systems tended to return time values as an array, which is why I used a small array (timevec) to hold the time values from the RTC. Glory be, the standard C time.h library has a function gmtime that returns Universal Time (historical GMT) ! Looks like structs are used for the data, that is sensible, I guess it follows the origins. Also there are utility functions for time differences and daylight saving. The conventional way seems to be to convert the date/time and difference to seconds from the start of epoch, subtract, then reconvert back to date/time. So the ripple calculation above is done as part of that. There is also the idea of using a date/time datatype, not justified here but it is used in say arduino etc. |
||||
Print this page |