gmtime always returns NULL?

03 Dec 2014

I am trying to get some time processing functions to work, and I am having difficulty getting gmtime to break a time_t value into a tm structure. I have the following code (having previously called set_time()):

time_t now = time(NULL);
struct tm * now_tm = gmtime(&now);
if( NULL==now_tm )
{
    printf("gmtime fail, now is %d\n",(int)now);
}

This returns the text "gmtime fail, now is 1417599213". That value looks correct, so I don't understand why gmtime() is returning NULL. Have I called it properly, or is there some other setup that needs performing?

12 Oct 2015

Did you resolve it because I meet it??

08 Nov 2016

I am also seeing that gmtime() is basically broken. Here is a simple test program that shows it:

test_gmtime.c

#include <stdio.h>
#include <string.h>
#include <time.h>

void test_gmtime(void)
{
    struct tm tm;
    struct tm *pT = NULL;
    time_t utc;
    memset(&tm, 0, sizeof(tm));

    tm.tm_sec = 5;
    tm.tm_min = 10;
    tm.tm_hour = 9;
    tm.tm_mday = 8;          /* Day-of-month. */
    tm.tm_mon = 11 - 1;       /* month 0..11 */
    tm.tm_year = 2016 - 1900;   /* years from 1900 */

    utc = mktime(&tm);

    printf("ctime = %s", ctime(&utc));

    pT = gmtime(&utc);

    if(pT == NULL) {
        printf("NULL answer\n");
    } else {
        printf("gmtime = %d-%d-%d %d:%d:%d\n",
               pT->tm_year + 1900,
               pT->tm_mon + 1,
               pT->tm_mday,
               pT->tm_hour,
               pT->tm_min,
               pT->tm_sec);
    }
}

int main()
{
	test_gmtime();
	return 0;
}

If you compile and run this on a normal PC (e.g. Linux here, but same for windows) it works:

gcc -Wall test_gmtime.c

./a.out

ctime = Tue Nov 8 09:10:05 2016

gmtime = 2016-11-8 9:10:5

But if run in the mded compiler on a target device (in my case DISCO_L476VG) the ctime() call works fine, but the gmtime() call is broken:

ctime = Tue Nov 8 09:10:05 2016

NULL answer

Is anyone going to explain what is wrong with the function?

09 Nov 2016

Online builds use the ARM compiler v5.06 which doesn't support gmtime(). Taken from http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0378f/chr1358938934656.html:

"Local time zones and Daylight Saving Time are not implemented. The values returned indicate that the information is not available. For example, the gmtime() function always returns NULL."