Skip to content

Commit

Permalink
Reuse tm_writer in chrono_formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Nov 5, 2021
1 parent f931033 commit 7c08236
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,7 @@ struct chrono_formatter {
bool negative;

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

explicit chrono_formatter(FormatContext& ctx, OutputIt o,
std::chrono::duration<Rep, Period> d)
Expand Down Expand Up @@ -1468,11 +1469,14 @@ 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 <class Callback, class... Args>
void format_tm(const tm& time, Callback cb, Args... args) {
if (isnan(val)) return write_nan();
const auto& loc = localized ? context.locale().template get<std::locale>()
: std::locale::classic();
out = detail::write<char_type>(out, time, loc, format, modifier);
auto w = tm_writer_type(loc, out, time);
(w.*cb)(std::forward<Args>(args)...);
out = w.out();
}

void on_text(const char_type* begin, const char_type* end) {
Expand Down Expand Up @@ -1513,7 +1517,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 @@ -1522,7 +1526,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 @@ -1531,7 +1535,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 @@ -1556,12 +1560,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 @@ -1585,7 +1589,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

0 comments on commit 7c08236

Please sign in to comment.