diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 8125fd35..a738aedb 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1460,6 +1460,14 @@ template struct tm_formatter { void write2(size_t value) { out = std::copy_n(detail::digits2(value), 2, out); } + void write_year(int year) { + if (year >= 0 && year < 10000) { + write2(to_unsigned(year / 100)); + write2(to_unsigned(year % 100)); + } else { + out = detail::write(out, year); + } + } explicit tm_formatter(FormatContext& ctx_, OutputIt out_, const std::tm& tm_) : ctx(ctx_), out(out_), tm(tm_) {} @@ -1550,7 +1558,7 @@ template struct tm_formatter { void on_tz_name() { format_localized('Z'); } void on_year(numeric_system ns) { if (ns == numeric_system::standard) { - out = detail::write(out, tm_year()); + write_year(tm_year()); } else { format_localized('Y', 'E'); } @@ -1607,9 +1615,7 @@ template struct tm_formatter { format_localized('V', 'O'); } } - void on_iso_week_based_year() { - out = detail::write(out, tm_iso_weak().year); - } + void on_iso_week_based_year() { write_year(tm_iso_weak().year); } void on_iso_week_based_year_last2() { write2(detail::to_unsigned(tm_split_year(tm_iso_weak().year).lower)); } diff --git a/test/chrono-test.cc b/test/chrono-test.cc index a9bd3053..a2a84ca9 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -60,6 +60,11 @@ TEST(chrono_test, format_tm) { EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25"); EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33"); + // Short year + tm.tm_year = 999 - 1900; + EXPECT_EQ(fmt::format("{:%Y}", tm), "0999"); + EXPECT_EQ(fmt::format("{:%G}", tm), "0998"); + // for week on the year // https://www.cl.cam.ac.uk/~mgk25/iso-time.html std::vector str_tm_list = {