parsing time doesn't work

11 Dec 2010

I'm trying to use the localtime() function to read the data provided by an NTP server.

When executing the following code (after receiving the current time via NTP):

time_t rawtime;
struct tm * timeinfo;

time ( &rawtime );
timeinfo = localtime ( &rawtime );

printf("Current time is : %s\n", ctime(&rawtime));

int currentYear=timeinfo->tm_year;
printf("current year=%d\n",currentYear);
I get as output:

Current time is : Sat Dec 11 22:28:25 2010
current year=110
When using strftime to print the content of the timeinfo data, I get 10 for the year. When replacing localtime() in the code above with gmtime(), the year is reported as 667.

So, what am I doing wrong? Is it that there is no time zone handling that localtime doesn't work, or is it something else?

Thanks,

hli

12 Dec 2010

To answer at least one part of my question: tm_year is defined as 'years since 1990', so 110 is the correct answer.

But gmtime() still doesn't work (all fields of the tm struct are set to bogus values), and localtime() does return only UTC time (which is what gmtime should do).

14 Dec 2012

gmtime() is only work always return NULL.