diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 46a93fde..ef6a2990 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -2071,9 +2071,9 @@ struct formatter, const auto subsecs = std::chrono::duration_cast( epoch - std::chrono::duration_cast(epoch)); - return formatter::format( + return formatter::do_format( localtime(std::chrono::time_point_cast(val)), - ctx, subsecs); + ctx, &subsecs); } return formatter::format( @@ -2117,6 +2117,22 @@ template struct formatter { return end; } + template + auto do_format(const std::tm& tm, FormatContext& ctx, + const Duration* subsecs) const -> decltype(ctx.out()) { + const auto loc_ref = ctx.locale(); + detail::get_locale loc(static_cast(loc_ref), loc_ref); + auto w = detail::tm_writer( + loc, ctx.out(), tm, subsecs); + 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: FMT_CONSTEXPR auto parse(basic_format_parse_context& ctx) -> decltype(ctx.begin()) { @@ -2134,32 +2150,7 @@ template struct formatter { template auto format(const std::tm& tm, FormatContext& ctx) const -> decltype(ctx.out()) { - const auto loc_ref = ctx.locale(); - detail::get_locale loc(static_cast(loc_ref), loc_ref); - auto w = detail::tm_writer(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(); - } - - template - auto format(const std::tm& tm, FormatContext& ctx, - const Duration& subsecs) const -> decltype(ctx.out()) { - const auto loc_ref = ctx.locale(); - detail::get_locale loc(static_cast(loc_ref), loc_ref); - auto w = detail::tm_writer( - loc, ctx.out(), tm, &subsecs); - 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(); + return do_format(tm, ctx, nullptr); } };