title: How to neatly initialize struct tm from ctime
body: <p>Consider these two ways to get an epoch time from a date formatted as a string:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  int main() {
      struct tm tm_init = {0};
      strptime("2012-10-26 16:00", "%Y-%m-%dT %H:%M", &amp;tm_init);
      long epoch = mktime(&amp;tm_init);
  
      struct tm tm_rand;
      strptime("2012-10-26 16:00", "%Y-%m-%dT %H:%M", &amp;tm_rand);
      epoch = mktime(&amp;tm_rand);
  
      return 0;
  }
  </code></pre>
  
  <p>Source: <a href="http://ideone.com/3xMUm8" rel="nofollow">http://ideone.com/3xMUm8</a> . Essentially the difference is that tm_init is initialized with 0 and tm_rand is not. The first one triggers g++ (assuming -Wall and -W) to say:</p>
  
  <pre><code>test.cpp:4:24: warning: missing initializer for member ‘tm::tm_hour’ [-Wmissing-field-initializers]
  </code></pre>
  
  <p>and similar messages for other fields in the tm struct. However, if I leave out the initialization, like in the second case, valgrind tells me:</p>
  
  <pre><code>==9892== Conditional jump or move depends on uninitialised value(s)
  ==9892==    at 0x51E957C: __offtime (offtime.c:40)
  ==9892==    by 0x51EBBE7: __tz_convert (tzset.c:653)
  ==9892==    by 0x51E9EDB: __mktime_internal (mktime.c:310)
  ==9892==    by 0x400786: main (test.cpp:10)
  </code></pre>
  
  <p>Of course, the latter is way worse than the first, but I don't like warnings either. Of course, I can initialize all the fields manually, by writing some kind of tm factory; but that would require me to write code depending on the logic of mt, and I would need two write a factory (blegh).</p>
  
  <p>Is this a bug in ctime? Unfortunately I could not find (semi-)official documentation on strptime, that is, there is no cplusplus.com page on it. Am I missing something? WHat is the <em>good</em> way to parse a string to an epoch long?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>As suggested by some answers, I should set the tm_isdst manually to -1, 0 or 1. However this:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  int main() {
      struct tm tm;
      strptime("2012-10-26 16:00", "%Y-%m-%dT %H:%M", &amp;tm);
      tm.tm_isdst = 0;
      long epoch = mktime(&amp;tm);
      return 0;
  }
  </code></pre>
  
  <p>Results in valgrind saying this:</p>
  
  <pre><code>==11329== Conditional jump or move depends on uninitialised value(s)
  ==11329==    at 0x51E9917: __offtime (offtime.c:83)
  ==11329==    by 0x51EBBE7: __tz_convert (tzset.c:653)
  ==11329==    by 0x51EA513: __mktime_internal (mktime.c:310)
  ==11329==    by 0x40078D: main (test.cpp:7)
  </code></pre>
  
  <p>So, should I also set _offtime?</p>