Matthew Hoggan Matthew Hoggan - 16 days ago 11
C Question

timegm on struct tm: Where year = 70 mon = 1 mday = 1 does not return 0 seconds?

Given the following:

struct tm tm;
time_t gps_epoch_to_epoch_diff;

memset(&tm, 0, sizeof(struct tm));
tm.tm_year = 70;
tm.tm_mon = 1;
tm.tm_mday = 1;

gps_epoch_to_epoch_diff = timegm(&tm);
printf("sec = %lus\n", gps_epoch_to_epoch_diff);


I would expect the output to be


sec = 0s


However, I am getting:


sec = 2678400s


Why?

Answer
tm.tm_year = 70;
tm.tm_mon = 1;
tm.tm_mday = 1;

You are using 1 as month, which is february, as tm_mon is zero-based. Use 0 and you should get your desired result.

Comments