question

Sandeep-1467 avatar image
0 Votes"
Sandeep-1467 asked cooldadtx commented

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

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;

}

c++
· 7
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your code outputs "fail bit is set" for me.

Whereas
ss >> std::get_time( &timeInfo, "%Y/%m/%d" );
doesn't.

0 Votes 0 ·

Thank you for the response, but I could see that issue. Here is a screen shot attached.

89060-get-time.png


0 Votes 0 ·
get-time.png (155.0 KiB)

I don't doubt that it doesn't work for you. Presumably something's different in our environments, or version of VS. I'm using the current VS 2019 16.10 Preview 2 FWIW.

0 Votes 0 ·
Show more comments

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered cooldadtx commented

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?

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the response. Here is a screenshot.

89182-sdkversion.png


0 Votes 0 ·
sdkversion.png (75.3 KiB)

Not that it should matter but you seem to be missing a few VS updates. The C++ language standard just says Default but doesn't print out the version. I'm assuming ISO C++ 14 which is what my latest version is.

0 Votes 0 ·

I tried changing that to C++ 14 and C++ 17, but, there is no change in result.

0 Votes 0 ·
Show more comments