HowardHinnant / date

A date and time library based on the C++11/14/17 <chrono> header
Other
3.08k stars 669 forks source link

parse() - %F is not equivalent to %Y-%m-%d #733

Closed stilgarpl closed 2 years ago

stilgarpl commented 2 years ago

I'm using this code to parse date (ss is std::stringstream parameter with date string)

                    std::chrono::system_clock::time_point result;
                    ss >> date::parse(format, result);
                    if (ss.good()) return result;
                    //error handling...

When format is "%F" it correctly parses dates with leading zeroes, for example "2020-05-09", but it fails on dates without leading zeroes ("2020-5-9"). But if I change the format to full "%Y-%m-%d", it parses both dates correctly. Surprisingly, it works when one leading zero is omitted ("2020-5-09" parses correctly). https://en.cppreference.com/w/cpp/chrono/parse says that both formats should be equivalent, so it looks like a bug.

Here is proof: https://wandbox.org/permlink/blYXeBZbmDH9HvQT

HowardHinnant commented 2 years ago

In case someone is puzzled by this same issue:

The check ss.good() is not correct in this context. If eofbit is set, ss.good() == false. The correct check is !ss.fail(), which doesn't care if eofbit is set or not.