Is there a version of PutTimeStamp(ts.tdate.year, ts.tdate.month, ts.tdate.day) which takes the zxSeconds...
The reason I was trying to write these things was to calculate durations (timeB-timeA), so now I need to seperate the seconds back to YYMMDD-time etc.
I started (found on web) the routine below... but just checking if there is a better way with built in libs to do this... I got all excited about the PutTimeStamp() untill I saw the parameters. The code below is only a start, what I have right now, am I going in the right direction ? I will also study more the algorithm that Mike gave a link to.
Code: Select all
'~ #define SECS_DAY 86400 '~ (24L * 60L * 60L)
'~ #define EPOCH_YR 1970
'~ private function LEAPYEAR(byval year as byte) as byte
'~ LEAPYEAR = NOT ((year) mod 4) AND (((year) MOD 100) OR NOT((year) MOD 400))
'~ end function
'~ private function YEARSIZE(byval year as byte) as byte
'~ if LEAPYEAR(year) = 1 then
'~ YEARSIZE = 366
'~ else
'~ YEARSIZE = 365
'~ end if
'~ end function
// We could do this if ram was no issue:
//uint8_t monthlen(uint8_t isleapyear,uint8_t month){
//const uint8_t mlen[2][12] = {
// { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
// { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
// };
// return(mlen[isleapyear][month]);
//}
//
private const mlen(2)(12) = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ), _
(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 )
'~ private function monthlen(byval isleapyear as byte,byval month as byte) as byte
'~ if month = 1 then
'~ monthlen = 28+isleapyear
'~ exit function
'~ end if
'~ if month>6 then
'~ month = month - 1
'~ end if
'~ if (month MOD 2) = 1 then
'~ monthlen = 30
'~ exit function
'~ end if
'~ monthlen = 31
'~ end function
// gmtime -- convert calendar time (sec since 1970) into broken down time
// returns something like Fri 2007-10-19 in day and 01:02:21 in clock
// The return values is the minutes as integer. This way you can update
// the entire display when the minutes have changed and otherwise just
// write current time (clock). That way an LCD display needs complete
// re-write only every minute.
sub gmtime(byval time as, byref ts as MyTimeStamp)
uint16_t tm_year = EPOCH_YR;
uint8_t tm_sec,tm_min,tm_hour,tm_wday,tm_mon;
dayclock = time % SECS_DAY;
dayno = time / SECS_DAY;
dim tm_sec as byte = dayclock % 60UL
dim tm_min as byte = (dayclock % 3600UL) / 60;
dim tm_hour as byte = dayclock / 3600UL;
dim tm_wday as byte = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(tm_year)) {
dayno -= YEARSIZE(tm_year)
tm_year++
wend
tm_mon = 0
while dayno >= monthlen(LEAPYEAR(tm_year),tm_mon))
dayno -= monthlen(LEAPYEAR(tm_year),tm_mon)
tm_mon++
wend
i=0
while (i<3)
dstr[i]= pgm_read_byte(&(day_abbrev[tm_wday*3 + i]))
i = i + 1
wend
ts.tdate.year = tm_year
ts.tdate.month = tm_mon + 1
ts.tdate.day = dayno + 1
ts.ttime.hour = tm_hour
ts.ttime.minute = tm_min
ts.ttime.seconds = tm_sec
end sub