Reputation: 25593
If I try to set the day in a tm
with std::get_time
nothing happens but, input stream is in a fail state, which means that a parse error has been occured.
What is wrong with the following code?
{ // setting time works
std::tm t{};
std::istringstream ss("01:02:03");
ss.imbue(std::locale("de_DE"));
ss >> std::get_time(&t, "%H:%M:%S");
std::cout << ss.fail() << std::endl;
std::cout << std::put_time(&t, "%c") << '\n';
}
{ // setting day of month did not work
std::tm t{};
std::istringstream ss("2");
ss.imbue(std::locale("de_DE"));
ss >> std::get_time(&t, "%d");
std::cout << ss.fail() << std::endl;
std::cout << std::put_time(&t, "%c") << '\n';
}
Output:
0
Sun Jan 0 01:02:03 1900
1
Sun Jan 0 00:00:00 1900
Upvotes: 5
Views: 996
Reputation: 2858
You need to pass a leading zero to the day:
std::istringstream ss("02");
EDIT: Now I notice that according to cppreference:
parses the day of the month as a decimal number (range [01,31]), leading zeroes permitted but not required.
Maybe it's a bug?
EDIT: Bug report here
Upvotes: 5
Reputation: 13420
I'm not quiet sure about this and I suspect, that this is a bug in your libstdc++ implementation.
Let's look at the documentation for %d
specifier at cppreference
Parses the day of the month as a decimal number (range [01,31]), leading zeroes permitted but not required
Your code fails, but if you do instead this:
std::istringstream ss("02");
It will perfectly compile. When I try it on my local machine with g++ 5.4 it will produce the same error. You may try it with a newer gcc/libstdc++
Upvotes: 1