When using std::get_time, I get an exception when parsing a datetime at midnight or noon. I am using the %I format specifier instead of %H because the hour should be between 1-12. I am using Microsoft Visual Studio 2013.
#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstring>
int main()
{
std::time_t time;
std::string timeString = "1/5/15 12:00 AM";
std::string formatMask = "%m/%d/%y %I:%M %p";
std::tm tm;
std::memset(&tm, 0, sizeof(std::tm));
std::istringstream ss(timeString);
ss.exceptions(std::ios::failbit | std::ios::badbit);
try
{
ss >> std::get_time(&tm, formatMask.c_str());
time = std::mktime(&tm);
}
catch (const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
std::cout << time << std::endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire