Skip to content

Commit

Permalink
Remove duplicate implementation (#3144)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladislav Shchapov <[email protected]>

Signed-off-by: Vladislav Shchapov <[email protected]>
  • Loading branch information
phprus authored Oct 17, 2022
1 parent cd7202e commit f67dbc9
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -2071,9 +2071,9 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
const auto subsecs = std::chrono::duration_cast<Duration>(
epoch - std::chrono::duration_cast<std::chrono::seconds>(epoch));

return formatter<std::tm, Char>::format(
return formatter<std::tm, Char>::do_format(
localtime(std::chrono::time_point_cast<std::chrono::seconds>(val)),
ctx, subsecs);
ctx, &subsecs);
}

return formatter<std::tm, Char>::format(
Expand Down Expand Up @@ -2117,6 +2117,22 @@ template <typename Char> struct formatter<std::tm, Char> {
return end;
}

template <typename FormatContext, typename Duration>
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<bool>(loc_ref), loc_ref);
auto w = detail::tm_writer<decltype(ctx.out()), Char, Duration>(
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<Char>& ctx)
-> decltype(ctx.begin()) {
Expand All @@ -2134,32 +2150,7 @@ 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_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();
}

template <typename FormatContext, typename Duration>
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<bool>(loc_ref), loc_ref);
auto w = detail::tm_writer<decltype(ctx.out()), Char, Duration>(
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<FormatContext, std::chrono::seconds>(tm, ctx, nullptr);
}
};

Expand Down

0 comments on commit f67dbc9

Please sign in to comment.