Add std::locale support to std::tm formatter
This commit is contained in:
parent
22948841d6
commit
3b61933abe
@ -1744,7 +1744,9 @@ template <typename Char> struct formatter<std::tm, Char> {
|
|||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const std::tm& tm, FormatContext& ctx) const
|
auto format(const std::tm& tm, FormatContext& ctx) const
|
||||||
-> decltype(ctx.out()) {
|
-> 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);
|
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), tm);
|
||||||
if (spec_ == spec::year_month_day)
|
if (spec_ == spec::year_month_day)
|
||||||
w.on_iso_date();
|
w.on_iso_date();
|
||||||
|
|||||||
@ -536,10 +536,18 @@ TEST(chrono_test, weekday) {
|
|||||||
auto loc = get_locale("ru_RU.UTF-8");
|
auto loc = get_locale("ru_RU.UTF-8");
|
||||||
std::locale::global(loc);
|
std::locale::global(loc);
|
||||||
auto mon = fmt::weekday(1);
|
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("{}", mon), "Mon");
|
||||||
|
EXPECT_EQ(fmt::format("{:%a}", tm), "Mon");
|
||||||
|
|
||||||
if (loc != std::locale::classic()) {
|
if (loc != std::locale::classic()) {
|
||||||
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
|
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
|
||||||
Contains(fmt::format(loc, "{:L}", mon)));
|
Contains(fmt::format(loc, "{:L}", mon)));
|
||||||
|
EXPECT_THAT((std::vector<std::string>{"пн", "Пн", "пнд", "Пнд"}),
|
||||||
|
Contains(fmt::format(loc, "{:%a}", tm)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user