gpt4 book ai didi

c++ - std::get_time 有错误吗?

转载 作者:行者123 更新时间:2023-12-04 15:53:10 24 4
gpt4 key购买 nike

我正在解决网站上的问题。我的代码如下:

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;
}

当我输入 07:05:45PM 时,它返回 07:05:45。但我预计 19:05:45。怎么了?

更新

“if”语句的代码是错误的。一定是

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;
}

代码返回 "Parse failed" for "07:05:45PM" (g++ 5.4.0) 这也是出乎意料的。

最佳答案

这是 gcc 中的错误 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71367

大胆强调是我的。

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.

最后一条评论是这样说的:

Jakub Jelinek 2021-04-27 11:37:54 UTC

GCC 11.1 has been released, retargeting bugs to GCC 11.2.

关于c++ - std::get_time 有错误吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53110175/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com