Interpretation of tm_isdst field in struct tm

Time structure (struct tm) contains a calendar date and time broken down into its components. The structure contains nine members of type int (in any order), which are:

Member  Type    Meaning Range
tm_sec  int seconds after the minute    0-61*
tm_min  int minutes after the hour  0-59
tm_hour int hours since midnight    0-23
tm_mday int day of the month    1-31
tm_mon  int months since January    0-11
tm_year int years since 1900    
tm_wday int days since Sunday   0-6
tm_yday int days since January 1    0-365
tm_isdst    int Daylight Saving Time flag

The Daylight Saving Time flag (tm_isdst) is greater than zero if Daylight Saving Time is in effect, zero if Daylight Saving Time is not in effect, and less than zero if the information is not available.

My Specific Questions related to tm_isdst are as follows:

  1. For date(YYYY-MM-DD) 2016-03-16 (when DST is in effect), should tm_isdst be 1? or 0?

  2. For date(YYYY-MM-DD) 2016-01-16 (when DST was not in effect), should tm_isdst be 1? or 0?

  3. Daylight Saving Time (United States) 2016 began at 2:00 AM on Sunday, March 13. So at say 1:45AM on March 13, what would be value of tm_isdst? Who sets this value of flag and how?

  4. Is there any way to check when DST was in effect from some system call?


Solution 1:

The key issue is that a struct tm represents a time-stamp and its fields are not limited to 24 hours, 12 months 60 minutes, etc. The DST field does not have to match what clocks use in that timezone for that date. Of course, setting to not-the-usual values needs rectification for many applications.

For a local time-stamp, simple set all the fields (except tm_yday and tm_wday need not be set) and call mktime(). It will adjust all the fields to the usual range, including setting tm_isdst to 0 or 1.


  1. For date(YYYY-MM-DD) 2016-03-16 (when DST is in effect), should tm_isdst be 1? or 0?

    // set to 1 to indicate DST
    tm.tm_isdst = 1;
    
  2. For date(YYYY-MM-DD) 2016-01-16 (when DST was not in effect), should tm_isdst be 1? or 0?

    // set to 0 to indicate standard time
    tm.tm_isdst = 0;
    
  3. Daylight Saving Time (United States) 2016 began at 2:00 AM on Sunday, March 13. So at say 1:45AM on March 13, what would be value of tm_isdst? Who sets this value of flag and how?

    Call mktime() with tm.tm_isdst = -1; when unsure of DST setting. If code knows the struct tm is a standard time, set the tm_isdst field to 0. It makes no difference what time of year it is. Like-wise for DST, if code knows it is a DST timestamp, set the field to 1. Calling mktime() will adjust the field to the usually setting for the local timezone.

    Of course 1:45AM (with tm_isdst < 0) can be interpreted either way and mktime() will typically select 0 or 1.

    // set to 1 to indicate DST
    // set to 0 to indicate standard time
    // set to -1 to indicate DST is not known
    tm.tm_isdst = -1;
    mktime(&tm);  // This will adjust all the fields including tm_isdst
    
  4. Is there any way to check when DST was in effect from some system call?

    Yes. To determine if a struct tm should have tm_isdst as 0 or 1, call mktime()

    mktime(&tm);  // This will adjust the fields including tm_isdst
    printf("%d\n", tm.tm_isdst);
    

BTW: robust code checks the return value of mktime()

if (mktime(&tm) == -1) puts("Oops");

Solution 2:

  1. It should be 1 (or a positive value), because, as you say, DST is in effect.

  2. It should be 0, because, as you say, DST is not in effect.

  3. It should be 0, because DST was not in effect at that time.

  4. You can use localtime() to populate a struct tm for a given time, and then check what the value of that flag is. That's primarily why it's there.