mercredi 5 avril 2017

std::get_time on Visual 2015 does not fail on incorrect date

I'm executing the following code on Windows with Visual Studio 2015. Basically I use std::get_time to parse a date, but when the date is invalid, for example, a day greater than 31, it does not seem to set the fail bit on the stream.

I have tried this on Ubuntu with g++ 5.4.0 and it sets the fail bit and prints "Parsing failed!". Is this a bug on Windows or am I doing something wrong.

Thanks in advance!

std::string date = "2019-2-37 23:00:00"; // day (37) is wrong. 
std::string format = "%Y-%m-%d %H:%M:%S";
std::tm tm_object{};
std::istringstream input(date);        
input.imbue(std::locale(std::setlocale(LC_ALL, nullptr)));
input >> std::get_time(&tm_object, format.c_str());
if (input.fail()) 
{
    std::cout << "Parsing failed!";
}
else
{
    std::cout << "Parsing ok!\n";
    std::cout << "Day is : " << tm_object.tm_mday;
}

Aucun commentaire:

Enregistrer un commentaire