diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index e0f5792a2c000..8e65bf3641236 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1708,7 +1708,9 @@ template struct formatter { template auto format(const std::tm& tm, FormatContext& ctx) const -> decltype(ctx.out()) { - const auto& loc = std::locale::classic(); + const auto& loc_ref = ctx.locale(); + const auto& loc = + loc_ref ? loc_ref.template get() : std::locale::classic(); auto w = detail::tm_writer(loc, ctx.out(), tm); if (spec_ == spec::year_month_day) w.on_iso_date(); diff --git a/test/chrono-test.cc b/test/chrono-test.cc index a8e40cc7cc08e..7dfcdf10d422b 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -529,10 +529,18 @@ TEST(chrono_test, weekday) { auto loc = get_locale("ru_RU.UTF-8"); std::locale::global(loc); auto mon = fmt::weekday(1); + + auto tm = std::tm(); + tm.tm_wday = static_cast(mon.c_encoding()); + EXPECT_EQ(fmt::format("{}", mon), "Mon"); + EXPECT_EQ(fmt::format("{:%a}", tm), "Mon"); + if (loc != std::locale::classic()) { EXPECT_THAT((std::vector{"пн", "Пн", "пнд", "Пнд"}), Contains(fmt::format(loc, "{:L}", mon))); + EXPECT_THAT((std::vector{"пн", "Пн", "пнд", "Пнд"}), + Contains(fmt::format(loc, "{:%a}", tm))); } }