title: Read time from std::string as UTC time
body: <p>In a program of mine I have to read in times in several different formats. In all formats however the time is given in UTC (i.e. not in my local timezone). What would be the best way for a function that takes the date string and a format string and outputs a <code>std::time_t</code> timestamp?</p>
  
  <p>Currently I am using boost</p>
  
  <pre><code>#include "boost/date_time/gregorian/gregorian.hpp"
  #include "boost/date_time/posix_time/posix_time.hpp"
  
  std::time_t to_timestamp_utc(const std::string&amp; _dateString) {
      using namespace boost::gregorian;
      using namespace boost::posix_time;
      return to_time_t(ptime(from_undelimited_string(_dateString)));
  }
  </code></pre>
  
  <p>but this works only for the "YYYYMMDD" format. The standard library function <code>std::get_time</code> on the other hands assumes the input date to be formatted in my local time not UTC (or at least I haven't found a way to change it yet). Any suggestions are most welcome.</p>
  
  <p><strong>Current Solution</strong> based on Maxim Egorushkin suggestion.</p>
  
  <pre><code>std::time_t utc_to_timestamp(const std::string&amp; _dateString, const std::string&amp; _format) {
      // Set sec, min, hour to zero in case the format does not provide those
      std::tm timeStruct;
      timeStruct.tm_sec = 0;
      timeStruct.tm_min = 0;
      timeStruct.tm_hour = 0;
      char* const result = strptime(_dateString.c_str(), _format.c_str(), &amp;timeStruct);
      // Throw exception if format did not work
      REQUIRE(result == _dateString.c_str()+_dateString.size(), "Failed to parse dateTime.");
      return timegm(&amp;timeStruct);
  }
  </code></pre>