diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index c4903c36..65a6e6f2 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -863,6 +863,7 @@ struct chrono_formatter { FormatContext& context; OutputIt out; int precision; + bool localized = false; // rep is unsigned to avoid overflow. using rep = conditional_t::value && sizeof(Rep) < sizeof(int), @@ -957,7 +958,8 @@ struct chrono_formatter { void format_localized(const tm& time, char format, char modifier = 0) { if (isnan(val)) return write_nan(); - auto locale = context.locale().template get(); + auto locale = localized ? context.locale().template get() + : std::locale::classic(); auto& facet = std::use_facet>(locale); std::basic_ostringstream os; os.imbue(locale); @@ -1086,6 +1088,7 @@ struct formatter, Char> { using arg_ref_type = detail::arg_ref; arg_ref_type width_ref; arg_ref_type precision_ref; + bool localized = false; basic_string_view format_str; using duration = std::chrono::duration; @@ -1148,6 +1151,10 @@ struct formatter, Char> { else handler.on_error("precision not allowed for this argument type"); } + if (begin != end && *begin == 'L') { + ++begin; + localized = true; + } end = parse_chrono_format(begin, end, detail::chrono_format_checker()); return {begin, end}; } @@ -1182,6 +1189,7 @@ struct formatter, Char> { detail::chrono_formatter f( ctx, out, d); f.precision = precision_copy; + f.localized = localized; parse_chrono_format(begin, end, f); } return detail::write( diff --git a/test/chrono-test.cc b/test/chrono-test.cc index d03e92d3..9288a15f 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -228,11 +228,11 @@ auto format_tm(const std::tm& time, fmt::string_view spec, return os.str(); } -# define EXPECT_TIME(spec, time, duration) \ - { \ - auto jp_loc = std::locale("ja_JP.utf8"); \ - EXPECT_EQ(format_tm(time, spec, jp_loc), \ - fmt::format(loc, "{:" spec "}", duration)); \ +# define EXPECT_TIME(spec, time, duration) \ + { \ + auto jp_loc = std::locale("ja_JP.utf8"); \ + EXPECT_EQ(format_tm(time, spec, jp_loc), \ + fmt::format(jp_loc, "{:L" spec "}", duration)); \ } TEST(chrono_test, locale) {