This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

mktime behaviour

Hi,
I use mktime to convert date and time into a time_t value and I see a behavior, which I didn't expect.
When calling mktime with 29.02.2010, I would expect the function to return -1, but it returns a time_t value representing the 01.03.2010.

Is that the normal behavior?

  • mktime() is defined accept just about anything that fits into the input fields. To quote the standard on it:

    and the original values of the
    other components are not restricted to the ranges indicated above

    mktime normalizes all fields of the input struct tm to their respective validity ranges. That's the majority of its job. It's even allowed to use mktime() on 9999-99-99T99:99:99, and get a useful (to some extent) result.

    The return value of -1 only applies if the conversion from struct tm to time_t fails. That follows from this clause in the standard:

    The mktime function returns the specified calendar time encoded as a value of type
    time_t. If the calendar time cannot be represented, the function returns the value
    (time_t)(-1).

    if you follow the use of the term "calendar time" closely.

  • It's normal that it normalizes the parameters.

    From §7.23.2.3 of ISO/IEC 98:99:1999:
    "The original values of the tm_wday and tm_yday components of the structure are ignored, and the original values of the other components are not restricted to the ranges indicated above."

    This is what a Linux system writes about mktime():
    "If structure members are out-side their legal interval, they will be normalized (so that, e.g., 40 October is changed into 9 November)."