Fix year formatter

This commit is contained in:
Vladislav Shchapov 2021-10-10 21:08:04 +05:00 committed by Victor Zverovich
parent 79c00ad8f2
commit 4707373d33
2 changed files with 15 additions and 4 deletions

View File

@ -1460,6 +1460,14 @@ template <typename FormatContext, typename OutputIt> 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<char_type>(out, year);
}
}
explicit tm_formatter(FormatContext& ctx_, OutputIt out_, const std::tm& tm_)
: ctx(ctx_), out(out_), tm(tm_) {}
@ -1550,7 +1558,7 @@ template <typename FormatContext, typename OutputIt> struct tm_formatter {
void on_tz_name() { format_localized('Z'); }
void on_year(numeric_system ns) {
if (ns == numeric_system::standard) {
out = detail::write<char_type>(out, tm_year());
write_year(tm_year());
} else {
format_localized('Y', 'E');
}
@ -1607,9 +1615,7 @@ template <typename FormatContext, typename OutputIt> struct tm_formatter {
format_localized('V', 'O');
}
}
void on_iso_week_based_year() {
out = detail::write<char_type>(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));
}

View File

@ -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<std::string> str_tm_list = {