This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

gmtime in time.h not generating correct values

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

Parents Reply Children
  • 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.