We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi guys,
I have the following code for a Freescale KL25Z-based Cortex-M0+, which attempts to generate hours, minutes, seconds etc from an epoch time that I'm extracting from the RTC:
#include <MKL25Z4.H> #include <time.h> time_t epochTimeSnapshot; struct tm utcTimeSnapshot; void TimeTakeSnapshot( void ) { epochTimeSnapshot = RTC->TSR; utcTimeSnapshot = *gmtime( &epochTimeSnapshot ); }
I'm compiling this file against C90 and am not using microlib. I've not attempted to customise any of the time.h functions, as I am not using any that are processor independent, just gmtime().
I can confirm that from the RTC, the value 0x51CC6DF8 is being placed into epochTimeSnapshot, which is a correct epoch time from a few days ago as far as I understand it. The issue is that following the execution of gmtime, the utcTimeSnapshot structure is being populated as follows:
tm_sec = 0x1FFFFDA0 tm_min = 0x00000339 tm_hour = 0x00000341 tm_mday = 0x00000343 tm_mon = 0x00000000 tm_year = 0x00000000 tm_wday = 0x00000000 tm_yday = 0x00000000 tm_isdst = 0x00000000
These results are clearly not correct, however I can't determine why this is, as I don't have access to the source of the gmtime function.
Has anyone else experienced any issues like this before? Am I perhaps missing something?
Any advice is much appreciated.
Many thanks
Code doesn't look right, try
#include <MKL25Z4.H> #include <time.h> time_t epochTimeSnapshot; struct tm * utcTimeSnapshot; void TimeTakeSnapshot( void ) { epochTimeSnapshot = RTC->TSR; utcTimeSnapshot = gmtime( &epochTimeSnapshot ); // look at utcTimeSnapshot->tm_min }
Although rare, that code is valid, I actually had your example first but modified it to match one I had found whilst trying to resolve the issue, to reduce the number of potential mistakes.
I've further looked into the issue and it appears it may very well be a bug, as substituting the code with the following works perfectly:
#include <MKL25Z4.H> #include <time.h> time_t epochTimeSnapshot; struct tm utcTimeSnapshot; void TimeTakeSnapshot( void ) { epochTimeSnapshot = RTC->TSR; localtime_r( &epochTimeSnapshot, &utcTimeSnapshot ); }
I will have to explore this a little more when I have time and see if I can produce a simple test case.
I had the same issue.
basically if I have no GPS timestamp I take the local clock convert to Epoc then add my time offset in seconds and then convert it back to time data
when trying to use the gmtime I would get 0xFFFFFFF (-1) ended up using localtime() and so far it seems to be doing the job
/******************************************************* * Okay have to make a UTC time from the RTC time * and Time Zone ******************************************************/ hw_get_time(&sRTCTime); hw_get_date(&sRTCDate); if(gsSystem.sNVM.wTimeOffset != 0) { time_t lPochSeconds; /************/ sTime.tm_hour = sRTCTime.RTC_Hours; sTime.tm_min = sRTCTime.RTC_Minutes; sTime.tm_sec = sRTCTime.RTC_Seconds; sTime.tm_mday = sRTCDate.RTC_Date; sTime.tm_mon = sRTCDate.RTC_Month; sTime.tm_year = ((sRTCDate.RTC_Year + 2000)-1900); /************************ * Get seconds since Epoc ************************/ lPochSeconds = mktime (&sTime); /* call mktime: timeinfo->tm_wday will be set */ /*********************************** * Add the time Zone adjustment in * 15 minute offsets per count & * convert to seconds (as ublox G100 TMZ command) ************************************/ lPochSeconds -= ((15*60)*gsSystem.sNVM.wTimeOffset); psTime = localtime ( &lPochSeconds ); memcpy(&sTime,psTime,sizeof(struct tm)); gsGSM.sHMI.bGpsHours = sTime.tm_hour; gsGSM.sHMI.bGpsminutes = sTime.tm_min; gsGSM.sHMI.bGpsSeconds = sTime.tm_sec; gsGSM.sHMI.bGpsDate = sTime.tm_mday; gsGSM.sHMI.bGpsMonth = sTime.tm_mon; gsGSM.sHMI.bGpsYear = ((sTime.tm_year+1900)-2000); } else /** no time correction so give RTC time **/ { gsGSM.sHMI.bGpsHours = sRTCTime.RTC_Hours; gsGSM.sHMI.bGpsminutes = sRTCTime.RTC_Minutes; gsGSM.sHMI.bGpsSeconds = sRTCTime.RTC_Seconds; gsGSM.sHMI.bGpsDate = sRTCDate.RTC_Date; gsGSM.sHMI.bGpsMonth = sRTCDate.RTC_Month ; gsGSM.sHMI.bGpsYear = sRTCDate.RTC_Year; } /** Mark we have a GPS time **/ gsGSM.bGpsTimeUpdate = C_GPS_TIME_SET_BY_RTC; }