From b5123182f9946ceb5a85e427b4577ba94ee46322 Mon Sep 17 00:00:00 2001 From: Patrick Roocks Date: Fri, 23 Sep 2022 09:09:25 +0200 Subject: [PATCH] Support formatting of std time_point with utc_clock --- include/fmt/chrono.h | 26 ++++++++++++++++++++++++++ test/chrono-test.cc | 9 +++++++++ 2 files changed, 35 insertions(+) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index ed7f5f1695cd4..605bcb766aa6a 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -22,6 +22,13 @@ FMT_BEGIN_NAMESPACE +// 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) +# endif +#endif + // Enable tzset. #ifndef FMT_USE_TZSET // UWP doesn't provide _tzset. @@ -2014,6 +2021,25 @@ struct formatter, } }; +#if (FMT_USE_UTC_TIME) +template +struct formatter, + Char> : formatter { + FMT_CONSTEXPR formatter() { + basic_string_view default_specs = + detail::string_literal{}; + this->do_parse(default_specs.begin(), default_specs.end()); + } + + template + auto format(std::chrono::time_point val, + FormatContext& ctx) const -> decltype(ctx.out()) { + return formatter::format( + localtime(std::chrono::utc_clock::to_sys(val)), ctx); + } +}; +#endif + template struct formatter { private: enum class spec { diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 0f2a2498e0155..8e0d008cb4d5b 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -641,3 +641,12 @@ TEST(chrono_test, cpp20_duration_subsecond_support) { } #endif // FMT_STATIC_THOUSANDS_SEPARATOR + +#if (FMT_USE_UTC_TIME) +TEST(chrono_test, utc_clock) { + auto t1 = std::chrono::system_clock::now(); + auto t1_utc = std::chrono::utc_clock::from_sys(t1); + EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t1), + fmt::format("{:%Y-%m-%d %H:%M:%S}", t1_utc)); +} +#endif