I'm solving a problem on a site. My code is the following:
string timeConversion(string s) {
std::tm t = {};
string result = "";
std::istringstream ss(s);
ss >> std::get_time(&t, "%I:%M:%S%p");
if (ss.fail()) {
std::ostringstream oss;
oss << std::put_time(&t, "%H:%M:%S") << endl;
result = oss.str();
} else {
result = "Parse failed";
}
return result;
}
When I input 07:05:45PM it returns 07:05:45 . But I expect 19:05:45 . What is wrong?
UPDATE
The code is wrong for "if" statement. It must be
string timeConversion(string s) {
std::tm t = {};
string result = "";
std::istringstream ss(s);
ss >> std::get_time(&t, "%I:%M:%S%p");
if (ss.fail()) {
result = "Parse failed";
} else {
std::ostringstream oss;
oss << std::put_time(&t, "%H:%M:%S") << endl;
result = oss.str();
}
return result;
}
The code returns "Parse failed" for "07:05:45PM" (g++ 5.4.0) It's unexpected too.
It is a bug in gcc https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71367
Bold emphasis is mine.
Ed Beroset 2016-05-31 21:17:27 UTC
Created attachment 38616 [details] Short program demonstrating lack of %r support in std::time_get
The "%r" and "%p" formats don't seem to be supported yet for time_get , even though they are both supported by time_put. The %p is supposed to be the locale's version of "AM" and "PM" (if any) and %r is equivalent to %I:%M:%S %p in the POSIX locale.
The last comment its saying:
Jakub Jelinek 2021-04-27 11:37:54 UTC
GCC 11.1 has been released, retargeting bugs to GCC 11.2.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.