title: Daylight savings and mktime in C using gcc under Linux
body: <p>I am working with detecting Daylight saving time (summer time) transitions.
  I have come across something that I don't understand, and was hoping for some explanation.</p>
  
  <p>I have stripped down my code to a nearly bare minimum to show the problem.   </p>
  
  <pre><code>int main(void)
  {
    struct tm tm1,tm2;
    time_t time1, time2;
    int order=0;//change this betwee 0 and 1
  
    tm1.tm_hour=2;
    if (order)
    {
      tm1.tm_hour=1;
    }
    tm1.tm_min=0;
    tm1.tm_sec=0;
    tm1.tm_mday=1;
    tm1.tm_mon=10;
    tm1.tm_year=115;  
    tm1.tm_isdst=-1;
    time1=mktime(&amp;tm1);
  
    //insert here
  
    tm2.tm_hour=1;
    if (order)
    {
      tm2.tm_hour=2;
    }
    tm2.tm_min=0;
    tm2.tm_sec=0;
    tm2.tm_mday=1;
    tm2.tm_mon=10;
    tm2.tm_year=115;  
    tm2.tm_isdst=-1;
    time2=mktime(&amp;tm2);
  
  
    printf("\n\n time stamp 1=%zu time stamp 2=%zu difference=%zi\n\n",time1 ,time2, time2-time1);
    exit(0);
  }
  </code></pre>
  
  <p>The output when order = 0 is:</p>
  
  <pre><code>time stamp 1=1446368400 time stamp 2=1446364800 difference=-3600
  </code></pre>
  
  <p>The output when order = 1 is:</p>
  
  <pre><code>time stamp 1=1446361200 time stamp 2=1446368400 difference=7200
  </code></pre>
  
  <p>(note that this is intentionally the time of the end of daylight saving time, one second after 01:59:59 on November 1st of 2015, it will be 01:00:00.)</p>
  
  <p>To put it simply, the conversion of the structure when hour = 2 depends on the conversion that occurred immediately before it. One o'clock can (correctly) be either 1446364800 (standard time) or 1446361200 (DST). if the conversion before is found to DST, the second choice is used and vice versa. The obvious solution is that mktime sets some variable, that it uses the next time. However I can't find any record of it. mktime does set three (four?) external variables, tzname[0],tzname[1], timezone and daylight, but these don't seem to be causing the effect. (I did a more complicated version of my test program to test for that.)  </p>
  
  <p>My time zone is America/Edmonton ( MST(UTC-7)/MDT(UTC -6) )</p>
  
  <pre><code>$ gcc --version&lt;br&gt;
  gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
  </code></pre>
  
  <p>Kubuntu 14.04 LTS</p>
  
  <p>Any insight or pointers would be much appreciated.</p>
  
  <p>Edit:
  replys to comments:
  1) Jonathan -- glibc version 2.19
  2) chux -- printing the values of tmX.tm_isdst, tmX.tm_hour after the mktime() doesn't provide me any insight. (is that because I'm blind?) The one O'clock conversion shows an hour of one and a is_dst of either 0 or 1, exactly as infered by the timestamp. The two O'clock conversion, of course, shows an hour of 2 and an is_dst of 0.</p>
  
  <p>reply to chux's(?) answer: I wonder If I have not fully explained myself.
  I realize what is going on: There is an ambiguity, and it has to "guess". What I am wondering is why (and how) can it's guess depend on the previous conversion? </p>
  
  <p>second edit:</p>
  
  <p>to confirm Wumpus Q. Wumbley's answer inserted the following code in the place indicated in the code above:</p>
  
  <pre><code>tm0.tm_hour=0;
  tm0.tm_min=0;
  tm0.tm_sec=0;
  tm0.tm_mday=1;
  tm0.tm_mon=10;
  tm0.tm_year=115;  
  tm0.tm_isdst=-1;
  time0=mktime(&amp;tm0);
  </code></pre>
  
  <p>(Basically a throw away conversion) I now get two hours difference regardless of order, so I get the DST version either way.</p>
  
  <p>Thanks for the explanation.</p>