https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101152

            Bug ID: 101152
           Summary: std::get_time %Y accepts years with less than 4 digits
           Product: gcc
           Version: 11.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: development at jordi dot vilar.cat
  Target Milestone: ---

C++ specs for std::time_get facet rely on the C strptime for the do_get method,
and the C specs say that %Y includes the century, that is, requires 4 digits.

As a result, the following code shows an incorrect date instead of rejecting
it.

#include 
#include 
#include 

int main(int, char**)
{
    std::stringstream stream("February 2003");
    std::tm timestamp{};
    if (stream >> std::get_time(&timestamp, "%B %d %Y"))
    {
        std::cout << "tm_mon: " << timestamp.tm_mon << std::endl;
        std::cout << "tm_mday: " << timestamp.tm_mday << std::endl;
        std::cout << "tm_year: " << timestamp.tm_year + 1900 << std::endl;
    }
    else
    {
        std::cout << "N/A" << std::endl;
    }
    return 0;
}

This code prints:

tm_mon: 1
tm_mday: 20
tm_year: 1903

because the two first digits of the year are consumed by %d as the tm_mday
field, leaving remaining two digits for year, but should just print N/A instead
as the %Y should require four digits.

Reply via email to