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

            Bug ID: 128725
           Summary: chrono::parse: %EY, %Ey, %Oy and %EC give the wrong
                    year, %Om and %OM always fail
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dezhen.lu at student dot uni-tuebingen.de
  Target Milestone: ---

In the "C" locale, chrono::parse with the E or O modified year specifiers
returns a year that is off by 1900, and %Om and %OM never succeed. Programs
that use these forms to accept locale-specific dates get wrong years or parse
failures even in the "C" locale, where the forms should read the same as %Y,
%y, %C, %m and %M.

#include <chrono>
#include <iostream>
#include <sstream>
template <class T> void p(const char* f, const char* in) {
  T v{}; std::istringstream is(in); is >> std::chrono::parse(f, v);
  std::cout << f << " \"" << in << "\": "; if (is) std::cout << v << '\n'; else
std::cout << "failed\n";
}
int main() {
  using namespace std::chrono;
  p<year>("%EY", "2026"); p<year>("%EY", "26"); p<year>("%Ey", "26");
p<year>("%Oy", "26");
  p<year>("%EC%y", "2026"); p<month>("%Om", "11"); p<minutes>("%OM", "59");
}

$ g++ -std=c++20 t.cc && ./a.out
%EY "2026": 0126
%EY "26": -1874
%Ey "26": 0126
%Oy "26": 0126
%EC%y "2026": 0126
%Om "11": failed
%OM "59": failed

Expected:
%EY "2026": 2026
%EY "26": 0026
%Ey "26": 2026
%Oy "26": 2026
%EC%y "2026": 2026
%Om "11": Nov
%OM "59": 59min

Same output with GCC 16.2.0 on aarch64-apple-darwin and with 14.1, 14.3, 15.2
and trunk (20261113) on x86_64-linux-gnu, so it has been there since
chrono::parse was added.

In bits/chrono_io.h these cases read the field with time_get and then use
tm_year as the year (%EY), compare tm_year with 2000 (%Ey, %Oy), or use tm_year
as the century (%EC), but tm_year counts years since 1900. For %Om and %OM the
format passed to time_get is __fmt - 2, i.e. "Om" and "OM" without the '%'; %Od
and %OS pass __fmt - 3 and work.

%OH and %OI also fail, but that is disabled on purpose in the source, so it is
not part of this report.

The testcase and this report were written with help from Claude
(claude-opus-5-5).

Reply via email to