title: C++ time_t problem
body: <p>I'm having trouble with dates management in C++ (VS 2008).</p>
  <p>According to <a href="http://msdn.microsoft.com/en-us/library/323b6b3k%28v=VS.90%29.aspx" rel="noreferrer">MSDN specifications</a>, <code>time_t</code> represents:</p>
  <p><em>The number of seconds since January 1, 1970, 0:00 UTC</em></p>
  <p>therefore, I've written this piece of code:</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;time.h&gt;
  
  time_t GetDate(int year, int month, int day, int hour, int min, int sec)
  {
      time_t rawtime;
      struct tm * timeinfo;
      
      time ( &amp;rawtime );
      timeinfo = gmtime ( &amp;rawtime );
      timeinfo-&gt;tm_year = year - 1900;
      timeinfo-&gt;tm_mon = month - 1;
      timeinfo-&gt;tm_mday = day;
      timeinfo-&gt;tm_hour = hour;
      timeinfo-&gt;tm_min = min;
      timeinfo-&gt;tm_sec = sec;
      timeinfo-&gt;tm_isdst = 0; // disable daylight saving time
      
      time_t ret = mktime ( timeinfo );
      
      return ret;
  }
  
  int main ()
  {
      time_t time_0 = GetDate(1970,1,1,0,0,0);
      // time_0 == -1 !!!
      time_t time_1 = GetDate(1970,1,1,1,0,0);
      // time_1 == 0 !!!
      return 0;
  }
  </code></pre>
  <p>It seems to be shifted by 1 hour (i.e. zero time is January 1, 1970, 1:00 UTC).</p>
  <p>Initially, I thought the problem could come from the DayLightSaving flag, but it doesn't change by changing it.</p>
  <p>Am I doing something wrong ?</p>
  <p>Thanks in advance</p>
  <hr />
  <p>P.S.
  In theory, I might not mind the zero time value, because it's only a reference time.</p>
  <p>But I need to be sure about the value, because I'm porting the code to another language and I need to get exactly the same results.</p>
  <hr />
  <h2>EDIT:</h2>
  <p>here's the solution, thanks to <a href="https://stackoverflow.com/questions/3660983/c-time-t-problem/3661129#3661129">Josh Kelley Answer</a></p>
  <pre><code>time_t mktimeUTC(struct tm* timeinfo)
  {
      // *** enter in UTC mode
      char* oldTZ = getenv(&quot;TZ&quot;);
      putenv(&quot;TZ=UTC&quot;);
      _tzset();
      // ***
  
      time_t ret = mktime ( timeinfo );
  
      // *** Restore previous TZ
      if(oldTZ == NULL)
      {
          putenv(&quot;TZ=&quot;);
      }
      else
      {
          char buff[255];
          sprintf(buff,&quot;TZ=%s&quot;,oldTZ);
          putenv(buff);
      }
      _tzset();
      // ***
  
      return ret;
  }
  </code></pre>