Unexpected value returned from mktime()

814 views Asked by At

I prepare the following structure :

struct tm tDepartureTime;
tDepartureTime.tm_min = 24;
tDepartureTime.tm_hour = 13;
tDepartureTime.tm_mday = 11;
tDepartureTime.tm_mon = 2 - 1;
tDepartureTime.tm_year = 2017 - 1900;

then I use mktime() to get the number of seconds.

unsigned long qTime = mktime( &tDepartureTime );

but it returns me number 1731157832 which is timestamp equivalent for 09.11.2024. Where could be a problem?

1

There are 1 answers

1
Bart van Nierop On BEST ANSWER

Some fields of your tm structure are uninitialized. Specifically, these are tm_sec, tm_mday, tm_wday, tm_yday and tm_isdst.

Of these, you need to manually set, at the very least, tm_sec. If its value randomly ends up being very high, that explains the time far into the future.

You could also initialize the entire struct with zeroes by changing your first line into struct tm tDepartureTime = {0}. This is probably the best solution.