MSDN Forum: std::get_time on visual studio 2019 does not fail on incorrect date format

Sandeep 1 Reputation point
2021-04-19T11:42:06.09+00:00

As part of input validation, I'm using std::get_time() to validate the input date that is coming in to my application.
I'm assuming below logic should set fail bit due to incorrect format but that doesn't. So, what could be the problem?
I'm using Visual studio 2019.

This is the sample code.

int main() {
std::tm timeInfo;
std::memset(&timeInfo, 0, sizeof(timeInfo));
timeInfo.tm_isdst = -1;

std::istringstream ss("2021/12/31");    
ss >> std::get_time(&timeInfo, "%m/%d/%Y");
if (ss.fail() == true)
  cout << "fail bit is set"<<endl;
return 0;

}

Developer technologies | C++
{count} votes

1 answer

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-04-19T13:55:04.74+00:00

    get_time is part of the localization library and is impacted by things like locale. Out of curiosity what does the runtime say the output is for the time you believe to be invalid?

    What is the version of the C++ language your code is compiling for?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.