diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 93f335cd..65905fe8 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -28,15 +28,6 @@ template inline void _tzset(T...) {} FMT_BEGIN_NAMESPACE -// Check if std::chrono::local_t is available. -#ifndef FMT_USE_LOCAL_TIME -# ifdef __cpp_lib_chrono -# define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L) -# else -# define FMT_USE_LOCAL_TIME 0 -# endif -#endif - // Enable safe chrono durations, unless explicitly disabled. #ifndef FMT_SAFE_DURATION_CAST # define FMT_SAFE_DURATION_CAST 1 @@ -263,6 +254,20 @@ struct utc_clock { }; #endif +// Check if std::chrono::local_time is available. +#ifdef FMT_USE_LOCAL_TIME +// Use the provided definition. +#elif defined(__cpp_lib_chrono) +# define FMT_USE_LOCAL_TIME (__cpp_lib_chrono >= 201907L) +#else +# define FMT_USE_LOCAL_TIME 0 +#endif +#if FMT_USE_LOCAL_TIME +using local_t = std::chrono::local_t; +#else +struct local_t {}; +#endif + } // namespace detail template @@ -271,6 +276,9 @@ using sys_time = std::chrono::time_point; template using utc_time = std::chrono::time_point; +template +using local_time = std::chrono::time_point; + namespace detail { // Prevents expansion of a preceding token as a function-style macro. @@ -2377,33 +2385,7 @@ struct formatter, Char> : formatter { } }; -#if FMT_USE_LOCAL_TIME -template -struct formatter, Char> - : formatter { - FMT_CONSTEXPR formatter() { - this->format_str_ = detail::string_literal(); - } - - template - auto format(std::chrono::local_time val, FormatContext& ctx) const - -> decltype(ctx.out()) { - using period = typename Duration::period; - if (period::num != 1 || period::den != 1 || - std::is_floating_point::value) { - const auto epoch = val.time_since_epoch(); - const auto subsecs = detail::duration_cast( - epoch - detail::duration_cast(epoch)); - - return formatter::do_format(localtime(val), ctx, &subsecs); - } - - return formatter::format(localtime(val), ctx); - } -}; -#endif - -template +template struct formatter, Char> : formatter, Char> { template @@ -2414,6 +2396,27 @@ struct formatter, Char> } }; +template +struct formatter, Char> : formatter { + FMT_CONSTEXPR formatter() { + this->format_str_ = detail::string_literal(); + } + + template + auto format(local_time val, FormatContext& ctx) const + -> decltype(ctx.out()) { + using period = typename Duration::period; + if (period::num == 1 && period::den == 1 && + !std::is_floating_point::value) { + return formatter::format(localtime(val), ctx); + } + auto epoch = val.time_since_epoch(); + auto subsecs = detail::duration_cast( + epoch - detail::duration_cast(epoch)); + return formatter::do_format(localtime(val), ctx, &subsecs); + } +}; + FMT_END_EXPORT FMT_END_NAMESPACE