Search code examples
cunixmktime

mktime() returning a huge amount of seconds


In a simple test getting the elapsed time in seconds since de Unix Epoch (01/01/1970 00:00:00 UTC) to 22 of February of 2019, it has returned a huge number of 18446744072104596016

Why not just 1550793600 seconds?

Isn't the time_t value portable?


The tm struct printed out in gdb console had the following values just before calling mktime():

(gdb) p ltm
$4 = {tm_sec = 0, tm_min = 0, tm_hour = 0, tm_mday = 22, tm_mon = 1, tm_year = 19, tm_wday = 0, tm_yday = 0, tm_isdst = 0}

Solution

  • Given

    tm_year = 19
    

    you are incorrectly using mktime() if you are expecting that input to represent the year 2019.

    Per 7.27.1 Components of time, paragraph 4 of the C standard:

    The tm structure shall contain at least the following members, in any order. The semantics of the members and their normal ranges are expressed in the comments.

         int    tm_sec;           //   seconds after the minute -- [0, 60]
         int    tm_min;           //   minutes after the hour -- [0, 59]
         int    tm_hour;          //   hours since midnight -- [0, 23]
         int    tm_mday;          //   day of the month -- [1, 31]
         int    tm_mon;           //   months since January -- [0, 11]
         int    tm_year;          //   years since 1900
         int    tm_wday;          //   days since Sunday -- [0, 6]
         int    tm_yday;          //   days since January 1 -- [0, 365]
         int    tm_isdst;         //   Daylight Saving Time flag
    

    Note that the tm_year value is denoted in years since 1900.

    You are likely getting the time_t value for Feb 22, 1919, which is -1604966400, or 18446744072104596016 unsigned.