Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does std::get_time have a bug?

Tags:

c++

g++

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.

like image 427
TigerTV.ru Avatar asked Dec 06 '25 11:12

TigerTV.ru


1 Answers

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.

like image 66
Alessandro Teruzzi Avatar answered Dec 07 '25 23:12

Alessandro Teruzzi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!