Skip to content

Commit

Permalink
changed locale retrieval way to a fancy one
Browse files Browse the repository at this point in the history
  • Loading branch information
toughengineer committed Nov 8, 2021
1 parent 0b843af commit 74855cb
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,26 @@ OutputIt format_duration_unit(OutputIt out) {
return out;
}

struct get_locale {
explicit get_locale(bool localized, locale_ref loc_ref)
: has_locale{localized} {
if (localized) locale = loc_ref.template get<std::locale>();
}
~get_locale() {
if (has_locale) locale.~locale();
}
operator const std::locale&() const {
return has_locale ? locale : get_classic_locale();
}

private:
union {
monostate dummy{};
std::locale locale;
};
bool has_locale = false;
};

template <typename FormatContext, typename OutputIt, typename Rep,
typename Period>
struct chrono_formatter {
Expand Down Expand Up @@ -1062,14 +1082,8 @@ struct chrono_formatter {

void format_localized(const tm& time, char format, char modifier = 0) {
if (isnan(val)) return write_nan();
if (localized) {
out = detail::write<char_type>(
out, time, context.locale().template get<std::locale>(), format,
modifier);
} else {
out = detail::write<char_type>(out, time, get_classic_locale(), format,
modifier);
}
out = detail::write<char_type>(
out, time, get_locale{localized, context.locale()}, format, modifier);
}

void on_text(const char_type* begin, const char_type* end) {
Expand Down Expand Up @@ -1236,11 +1250,8 @@ template <typename Char> struct formatter<weekday, Char> {
auto format(weekday wd, FormatContext& ctx) const -> decltype(ctx.out()) {
auto time = std::tm();
time.tm_wday = static_cast<int>(wd.c_encoding());
if (localized)
return detail::write<Char>(ctx.out(), time,
ctx.locale().template get<std::locale>(), 'a');
return detail::write<Char>(ctx.out(), time, detail::get_classic_locale(),
'a');
return detail::write<Char>(
ctx.out(), time, detail::get_locale{localized, ctx.locale()}, 'a');
}
};

Expand Down Expand Up @@ -1855,10 +1866,9 @@ template <typename Char> struct formatter<std::tm, Char> {
template <typename FormatContext>
auto format(const std::tm& tm, FormatContext& ctx) const
-> decltype(ctx.out()) {
if (const auto& loc_ref = ctx.locale())
return this->do_format(ctx.out(), tm,
loc_ref.template get<std::locale>());
return this->do_format(ctx.out(), tm, detail::get_classic_locale());
const auto loc_ref = ctx.locale();
return this->do_format(
ctx.out(), tm, detail::get_locale{static_cast<bool>(loc_ref), loc_ref});
}
};

Expand Down

0 comments on commit 74855cb

Please sign in to comment.