6 years, 9 months ago.

What is wrong with STRUCT TM and GMTIME ?

very strange things are introduced in a struct tm structure when using gmtime() function. I am using mbed online and nucleo F401RE board

int Calendar::findNextDelay(time_t * absoluteTime)
{
    printf("\nin findNextDelay, Time as a basic string = %s", ctime(absoluteTime));
    
    #define MST (-7)
    #define UTC (0)
    #define CCT (+8)

    time_t rawtime;
    struct tm * ptm;
    rawtime = *absoluteTime;

    ptm = gmtime ( &rawtime );

    puts ("Current time around the World:");
    printf ("Phoenix, AZ (U.S.) :  %d:%d\n", (ptm->tm_hour+MST)%24, ptm->tm_min);
    printf ("Reykjavik (Iceland) : %d:%d\n", (ptm->tm_hour+UTC)%24, ptm->tm_min);
    printf ("Beijing (China) :     %d:%d\n", (ptm->tm_hour+CCT)%24, ptm->tm_min);

the results are:

in findNextDelay, Time as a basic string = Sun Jan 28 22:09:09 2018 Current time around the World: Phoenix, AZ (U.S.) : 14:134218165 Reykjavik (Iceland) : 21:134218165 Beijing (China) : 5:134218165

After different attempts, I cannot have real minutes or seconds in this structure, even if ctime seems to work well...

Thank you for any help !

It's << and >> for formatting :)

posted by Sam Grove 29 Jan 2018

1 Answer

6 years, 9 months ago.

Hi,

I tried your program on my FRDM-KL25Z, and minutes were also strange,
different value from yours but always the same wrong value as yours.
Then I ported your program on to my Cygwin, and it worked OK.
So I suspect that this is a bug of gmtime().
For the time being, may be we can survive by writing...

 int Calendar::findNextDelay(time_t * absoluteTime) 
{ 
    printf("\nin findNextDelay, Time as a basic string = %s", ctime(absoluteTime));
    
    time_t seconds  = *absoluteTime % 60 ;
    time_t minutes  = *absoluteTime / 60 ;
    time_t hours    = minutes / 60  ; 
    time_t days     = hours / 24 ;
    minutes -= hours * 60 ;
    hours   -= days * 24 ;
    
    puts ("Current time around the World:"); 
    printf ("Phoenix, AZ (U.S.)  : %02d:%02d\n", (hours+MST+24)%24, minutes); 
    printf ("Reykjavik (Iceland) : %02d:%02d\n", (hours+UTC+24)%24, minutes); 
    printf ("Beijing (China)     : %02d:%02d\n", (hours+CCT+24)%24, minutes);
}

moto

Hi Moto, thank you very much to have taken time on this. I do not feel anymore alone on this bug. Yes, I will try your proposal, and will probably survive (;-)

posted by PHILIPPE CLEMENT 29 Jan 2018