title: Dealing with daylight savings time - C++
body: <p>Given an input date and time (in string format), I am trying to get the epoch time for it using <code>time</code> functions given in <code>ctime</code> like <code>mktime</code>. Converting the <code>time_t</code> epoch time back to a date and time results in a date and time that is one hour less than the original. I have gone through some discussions which say that there may be an adjustment of one hour in case of daylight savings time. Here is the code:  </p>
  
  <pre><code>//sample strptime program.
  
  #include &lt;iostream&gt;
  #include &lt;ctime&gt;
  #include &lt;string&gt;
  using namespace std;
  
  long parseTime(string time) {
  
    cout &lt;&lt; "Time entered = " &lt;&lt; time &lt;&lt; endl;
  
    long timeSinceEpoch;
  
    struct tm t;
  
    if(time.find("/") != string::npos) {
      //format of date is mm/dd/yyyy. followed by clock in hh:mm (24 hour clock).
      if(strptime(time.c_str(), "%m/%e/%Y %H:%M", &amp;t) == NULL) {
        cout &lt;&lt; "Error. Check string for formatting." &lt;&lt; endl;
      }
    } else if(time.find("-") != string::npos) {
      //format of date is yyyy-mm-dd hh:mm:ss (hh in 24 hour clock format).
      cout &lt;&lt; "I am here." &lt;&lt; endl;
      if(strptime(time.c_str(), "%Y-%m-%e %H:%M:%S", &amp;t) == NULL) {
        cout &lt;&lt; "Error. Check string for formatting of new date." &lt;&lt; endl;
      }
    }
  
    cout &lt;&lt; "Details of the time structure:" &lt;&lt; endl;
    cout &lt;&lt; "Years since 1900 = " &lt;&lt; t.tm_year &lt;&lt; endl;
    cout &lt;&lt; "Months since January = " &lt;&lt; t.tm_mon &lt;&lt; endl;
    cout &lt;&lt; "Day of the month = " &lt;&lt; t.tm_mday &lt;&lt; endl;
    cout &lt;&lt; "Hour = " &lt;&lt; t.tm_hour &lt;&lt; " Minute = " &lt;&lt; t.tm_min &lt;&lt; " second = " &lt;&lt; t.tm_sec &lt;&lt; endl;
  
    timeSinceEpoch = mktime(&amp;t);
    time_t temp = mktime(&amp;t);
    cout &lt;&lt; "Time since epoch = " &lt;&lt; timeSinceEpoch &lt;&lt; endl;
  
    cout &lt;&lt; "Reconverting to the time structure:" &lt;&lt; endl;
    struct tm* t2 = localtime(&amp;temp);
    cout &lt;&lt; "Details of the time structure:" &lt;&lt; endl;
    cout &lt;&lt; "Years since 1900 = " &lt;&lt; t2-&gt;tm_year &lt;&lt; endl;
    cout &lt;&lt; "Months since January = " &lt;&lt; t2-&gt;tm_mon &lt;&lt; endl;
    cout &lt;&lt; "Day of the month = " &lt;&lt; t2-&gt;tm_mday &lt;&lt; endl;
    cout &lt;&lt; "Hour = " &lt;&lt; t2-&gt;tm_hour &lt;&lt; " Minute = " &lt;&lt; t2-&gt;tm_min &lt;&lt; " second = " &lt;&lt; t2-&gt;tm_sec &lt;&lt; endl;
  
    return timeSinceEpoch;
  }
  
  int main(int argc, char *argv[]) {
  
    string date, t;
    cout &lt;&lt; "Enter date: " &lt;&lt; endl;
    cin &gt;&gt; date;
    cout &lt;&lt; "Enter time: " &lt;&lt; endl;
    cin &gt;&gt; t;
  
    struct tm time;
    string overall = date + " " + t;
  
    long result = parseTime(overall);
    cout &lt;&lt; "Time in date + time = " &lt;&lt; overall &lt;&lt; " and since epoch = " &lt;&lt; result &lt;&lt; endl;
  
  return 0;
  }
  </code></pre>
  
  <p>The troublesome input is:
  date: 2013-03-11
  time: 04:41:53</p>
  
  <p>My questions(s):<br>
  1. Checking for <code>tm_idst</code> flag returns a non-zero, indicating that DST is in effect. However, how do I know what timezone is being talked about?<br>
  2. The timestamp given above may not have been recorded in the same time zone as I am in. Is there a way to specify timezone so that the <code>tm_idst</code> flags are correctly set?<br>
  3. How do I deal with DST when I am unsure of what timezone the timestamp was recorded in? </p>