#include <vector>
#include <string>
#include <iostream>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <cstring>
int main()
{
std::vector<std::string> vec;
vec.push_back("2016:07:30 09:27:06");
vec.push_back("2016:07:30 09:27:07");
vec.push_back("2016:07:30 09:27:08");
for (auto & i : vec)
{
struct std::tm tm;
/* std::sscanf() method: */
std::memset(&tm, 0, sizeof(tm));
if (6 != std::sscanf(i.c_str(), "%d:%d:%d %d:%d:%d",
&tm.tm_year, &tm.tm_mon, &tm.tm_mday,
&tm.tm_hour, &tm.tm_min, &tm.tm_sec))
{
return -1;
}
/* correct the numbers according to:
* see: http://e...content-available-to-author-only...e.com/w/cpp/chrono/c/tm */
--tm.tm_mon;
tm.tm_year -= 1900;
/* mktime determines if Daylight Saving Time was in effect
* see: http://e...content-available-to-author-only...e.com/w/cpp/chrono/c/mktime */
tm.tm_isdst = -1;
std::time_t time = std::mktime(&tm);
std::cout << i << " | sscanf() time = " << time << std::endl;
/************************************************************/
/* std::get_time() method: */
std::istringstream iss;
iss.str(i);
iss >> std::get_time(&tm, "%Y:%m:%d %H:%M:%S");
time = std::mktime(&tm);
std::cout << i << " | std::get_time() time = " << time << std::endl;
}
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0cmluZz4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxpb21hbmlwPgojaW5jbHVkZSA8c3N0cmVhbT4KI2luY2x1ZGUgPGNzdHJpbmc+CgppbnQgbWFpbigpCnsKICAgc3RkOjp2ZWN0b3I8c3RkOjpzdHJpbmc+IHZlYzsKCiAgIHZlYy5wdXNoX2JhY2soIjIwMTY6MDc6MzAgMDk6Mjc6MDYiKTsKICAgdmVjLnB1c2hfYmFjaygiMjAxNjowNzozMCAwOToyNzowNyIpOwogICB2ZWMucHVzaF9iYWNrKCIyMDE2OjA3OjMwIDA5OjI3OjA4Iik7CgogICBmb3IgKGF1dG8gJiBpIDogdmV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