mktime and tm_isdst

hari picture hari · Dec 19, 2011 · Viewed 31.3k times · Source

I saw a lot of different views so thought of asking here.

I read man mktime:

 (A positive or zero value for tm_isdst causes mktime() to presume initially
 that summer time (for example, Daylight Saving Time) is or is not in
 effect for the specified time, respectively.  A negative value for
 tm_isdst causes the mktime() function to attempt to divine whether summer
 time is in effect for the specified time. 

My question is, shouldn't tm_isdst be kept as -1 to let the system decide if its dst or not and that way the code becomes dst agnostic?

Am I missing something?

Answer

Rich Jahn picture Rich Jahn · Aug 24, 2012

You should avoid setting tm_isdst to -1 if possible. The system can't always determine DST status from date and time alone. It is ambiguous the hour before and after DST ends. For example, if you pass mktime() 1:30 AM November 4, 2012, that's not enough information to get a correct time_t value from mktime(). Usually I have seen mktime() assume standard time in the case that it is ambiguous, but I haven't seen any documentation that guarantees that behavior on all platforms. 1:30 AM November 4, 2012 with tm_isdst == 1 would be 1 hour before, because the hour 1:00:00 to 1:59:59 repeats.

#include <stdio.h>
#include <time.h>

int main()
{
    time_t daylight, standard;
    struct tm timestr;
    double diff;

    timestr.tm_year = 2012 - 1900;
    timestr.tm_mon = 11 - 1;
    timestr.tm_mday = 4;
    timestr.tm_hour = 1;
    timestr.tm_min = 30;
    timestr.tm_sec = 0;

    /* first with standard time */
    timestr.tm_isdst = 0;
    standard = mktime(&timestr);

    /* now with daylight time */
    timestr.tm_isdst = 1;
    daylight = mktime(&timestr);

    diff = difftime(standard, daylight);

    printf("Difference is %f hour(s)", diff/60.0/60.0);

    return 0;
}

This produces:

Difference is 1.000000 hour(s)

Both are November 4, 2012 1:30 AM, however both are two distinct time_t values, 1 hour apart.

mktime() essentially has 2 outputs:

  • time_t
  • repaired time struct

The time struct is both an input and output. It is modified by mktime() to return all struct members to nominal ranges. For example, if you increment the tm_hour member += 500, that means increment the time by 500 hours. The tm_hour member will be changed to a value 00 to 23, and the tm_day, tm_mday, and etc will all be adjusted accordingly. tm_isdst is also both an input and output. Its values are as follows:

  • 1 (DST in effect, i.e. daylight time)
  • 0 (DST not in effect, i.e. standard time)
  • -1 (Unknown DST status)

So mktime() will output either a 1 or 0 for tm_isdst, never -1.

-1 is a possible input, but I would think of it as meaning "Unknown". Don't think of it as meaning "determine automatically", because in general, mktime() can't always determine it automatically.

The explicit DST status (0 or 1) should come from something external to the software, for example store it in the file or database, or prompt the user.