Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reuse tm_writer in other formatters #2586

Merged
merged 3 commits into from
Nov 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 27 additions & 26 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ struct chrono_formatter {
bool negative;

using char_type = typename FormatContext::char_type;
using tm_writer_type = tm_writer<OutputIt, char_type>;

chrono_formatter(FormatContext& ctx, OutputIt o,
std::chrono::duration<Rep, Period> d)
Expand Down Expand Up @@ -1493,10 +1494,13 @@ struct chrono_formatter {
void write_pinf() { std::copy_n("inf", 3, out); }
void write_ninf() { std::copy_n("-inf", 4, out); }

void format_localized(const tm& time, char format, char modifier = 0) {
template <typename Callback, typename... Args>
void format_tm(const tm& time, Callback cb, Args... args) {
if (isnan(val)) return write_nan();
out = detail::write<char_type>(
out, time, get_locale(localized, context.locale()), format, modifier);
get_locale loc(localized, context.locale());
auto w = tm_writer_type(loc, out, time);
(w.*cb)(args...);
out = w.out();
}

void on_text(const char_type* begin, const char_type* end) {
Expand Down Expand Up @@ -1537,7 +1541,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(hour(), 2);
auto time = tm();
time.tm_hour = to_nonnegative_int(hour(), 24);
format_localized(time, 'H', 'O');
format_tm(time, &tm_writer_type::on_24_hour, ns);
}

void on_12_hour(numeric_system ns) {
Expand All @@ -1546,7 +1550,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(hour12(), 2);
auto time = tm();
time.tm_hour = to_nonnegative_int(hour12(), 12);
format_localized(time, 'I', 'O');
format_tm(time, &tm_writer_type::on_12_hour, ns);
}

void on_minute(numeric_system ns) {
Expand All @@ -1555,7 +1559,7 @@ struct chrono_formatter {
if (ns == numeric_system::standard) return write(minute(), 2);
auto time = tm();
time.tm_min = to_nonnegative_int(minute(), 60);
format_localized(time, 'M', 'O');
format_tm(time, &tm_writer_type::on_minute, ns);
}

void on_second(numeric_system ns) {
Expand All @@ -1580,12 +1584,12 @@ struct chrono_formatter {
}
auto time = tm();
time.tm_sec = to_nonnegative_int(second(), 60);
format_localized(time, 'S', 'O');
format_tm(time, &tm_writer_type::on_second, ns);
}

void on_12_hour_time() {
if (handle_nan_inf()) return;
format_localized(time(), 'r');
format_tm(time(), &tm_writer_type::on_12_hour_time);
}

void on_24_hour_time() {
Expand All @@ -1609,7 +1613,7 @@ struct chrono_formatter {

void on_am_pm() {
if (handle_nan_inf()) return;
format_localized(time(), 'p');
format_tm(time(), &tm_writer_type::on_am_pm);
}

void on_duration_value() {
Expand Down Expand Up @@ -1663,8 +1667,10 @@ 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());
return detail::write<Char>(
ctx.out(), time, detail::get_locale(localized, ctx.locale()), 'a');
detail::get_locale loc(localized, ctx.locale());
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), time);
w.on_abbr_weekday();
return w.out();
}
};

Expand Down Expand Up @@ -1841,18 +1847,6 @@ template <typename Char> struct formatter<std::tm, Char> {
return end;
}

template <typename It>
It do_format(It out, const std::tm& tm, const std::locale& loc) const {
auto w = detail::tm_writer<It, Char>(loc, out, tm);
if (spec_ == spec::year_month_day)
w.on_iso_date();
else if (spec_ == spec::hh_mm_ss)
w.on_iso_time();
else
detail::parse_chrono_format(specs.begin(), specs.end(), w);
return w.out();
}

public:
template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
Expand All @@ -1862,9 +1856,16 @@ 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 = ctx.locale();
return this->do_format(ctx.out(), tm,
detail::get_locale(static_cast<bool>(loc), loc));
const auto loc_ref = ctx.locale();
detail::get_locale loc(static_cast<bool>(loc_ref), loc_ref);
auto w = detail::tm_writer<decltype(ctx.out()), Char>(loc, ctx.out(), tm);
if (spec_ == spec::year_month_day)
w.on_iso_date();
else if (spec_ == spec::hh_mm_ss)
w.on_iso_time();
else
detail::parse_chrono_format(specs.begin(), specs.end(), w);
return w.out();
}
};

Expand Down