Skip to content

Commit

Permalink
Cleanup chrono formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Aug 18, 2024
1 parent 0e741e0 commit d87b323
Showing 1 changed file with 26 additions and 19 deletions.
45 changes: 26 additions & 19 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,6 @@ FMT_BEGIN_NAMESPACE
# endif
#endif

// Check if std::chrono::utc_timestamp is available.
#ifndef FMT_USE_UTC_TIME
# ifdef __cpp_lib_chrono
# define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L)
# else
# define FMT_USE_UTC_TIME 0
# endif
#endif

// Enable safe chrono durations, unless explicitly disabled.
#ifndef FMT_SAFE_DURATION_CAST
# define FMT_SAFE_DURATION_CAST 1
Expand Down Expand Up @@ -254,11 +245,28 @@ auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
} // namespace safe_duration_cast
#endif

namespace detail {

// Check if std::chrono::utc_time is available.
#ifdef FMT_USE_UTC_TIME
// Use the provided definition.
#elif defined(__cpp_lib_chrono)
# define FMT_USE_UTC_TIME (__cpp_lib_chrono >= 201907L)
#else
# define FMT_USE_UTC_TIME 0
#endif
#if FMT_USE_UTC_TIME
using utc_clock = std::chrono::utc_clock;
#else
struct utc_clock {
void to_sys();
};
#endif

// Prevents expansion of a preceding token as a function-style macro.
// Usage: f FMT_NOMACRO()
#define FMT_NOMACRO

namespace detail {
template <typename T = void> struct null {};
inline auto localtime_r FMT_NOMACRO(...) -> null<> { return null<>(); }
inline auto localtime_s(...) -> null<> { return null<>(); }
Expand Down Expand Up @@ -2390,21 +2398,20 @@ struct formatter<std::chrono::local_time<Duration>, Char>
};
#endif

#if FMT_USE_UTC_TIME
template <typename Duration>
using utc_time = std::chrono::time_point<detail::utc_clock, Duration>;

template <typename Char, typename Duration>
struct formatter<std::chrono::time_point<std::chrono::utc_clock, Duration>,
Char>
struct formatter<utc_time<Duration>, Char>
: formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
Char> {
template <typename FormatContext>
auto format(std::chrono::time_point<std::chrono::utc_clock, Duration> val,
FormatContext& ctx) const -> decltype(ctx.out()) {
return formatter<
std::chrono::time_point<std::chrono::system_clock, Duration>,
Char>::format(std::chrono::utc_clock::to_sys(val), ctx);
auto format(utc_time<Duration> val, FormatContext& ctx) const
-> decltype(ctx.out()) {
using tp = std::chrono::time_point<std::chrono::system_clock, Duration>;
return formatter<tp, Char>::format(detail::utc_clock::to_sys(val), ctx);
}
};
#endif

FMT_END_EXPORT
FMT_END_NAMESPACE
Expand Down

0 comments on commit d87b323

Please sign in to comment.