简体   繁体   English

std::get_time 有错误吗?

[英]does std::get_time have a bug?

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 .当我输入07:05:45PM它返回07:05:45 But I expect 19:05:45 .但我预计19:05:45 What is wrong?怎么了?

UPDATE更新

The code is wrong for "if" statement. “if”语句的代码是错误的。 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.代码为“07:05:45PM”返回“Parse failed” (g++ 5.4.0)这也出乎意料。

It is a bug in gcc https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71367这是 gcc https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71367 中的一个错误

Bold emphasis is mine.大胆的强调是我的。

Ed Beroset 2016-05-31 21:17:27 UTC埃德·贝罗塞特 2016-05-31 21:17:27 UTC

Created attachment 38616 [details] Short program demonstrating lack of %r support in std::time_get已创建附件 38616 [详细信息] 演示在 std::time_get 中缺少 %r 支持的简短程序

The "%r" and "%p" formats don't seem to be supported yet for time_get , even though they are both supported by time_put. time_get 似乎还不支持 "%r" 和 "%p" 格式,即使它们都被 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. %p 应该是“AM”和“PM”(如果有)的语言环境版本,而 %r 相当于 POSIX 语言环境中的 %I:%M:%S %p。

The last comment its saying:最后一条评论说:

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

GCC 11.1 has been released, retargeting bugs to GCC 11.2. GCC 11.1 已发布,将错误重新定位到 GCC 11.2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM