Skip to content

Commit

Permalink
Add std::locale support to std::tm formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Oct 17, 2021
1 parent bbbb51d commit f8410da
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,9 @@ template <typename Char> struct formatter<std::tm, Char> {
template <typename FormatContext>
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>() : std::locale::classic();
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), tm);
if (spec_ == spec::year_month_day)
w.on_iso_date();
Expand Down
8 changes: 8 additions & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(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<std::string>{"пн", "Пн", "пнд", "Пнд"}),
Contains(fmt::format(loc, "{:L}", mon)));
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
Contains(fmt::format(loc, "{:%a}", tm)));
}
}

Expand Down

0 comments on commit f8410da

Please sign in to comment.