diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index c182f6f5..1a3b8d5e 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -325,6 +325,11 @@ inline std::tm localtime(std::time_t time) { return lt.tm_; } +inline std::tm localtime( + std::chrono::time_point time_point) { + return localtime(std::chrono::system_clock::to_time_t(time_point)); +} + // Thread-safe replacement for std::gmtime inline std::tm gmtime(std::time_t time) { struct dispatcher { @@ -361,6 +366,11 @@ inline std::tm gmtime(std::time_t time) { return gt.tm_; } +inline std::tm gmtime( + std::chrono::time_point time_point) { + return gmtime(std::chrono::system_clock::to_time_t(time_point)); +} + namespace detail { inline size_t strftime(char* str, size_t count, const char* format, const std::tm* time) { @@ -373,6 +383,17 @@ inline size_t strftime(wchar_t* str, size_t count, const wchar_t* format, } } // namespace detail +template +struct formatter, Char> + : formatter { + template + auto format(std::chrono::time_point val, + FormatContext& ctx) -> decltype(ctx.out()) { + std::tm time = localtime(val); + return formatter::format(time, ctx); + } +}; + template struct formatter { template auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { diff --git a/test/chrono-test.cc b/test/chrono-test.cc index b876c151..fa383c14 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -95,6 +95,17 @@ TEST(TimeTest, GMTime) { EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); } +TEST(TimeTest, TimePoint) { + std::chrono::system_clock::time_point point = std::chrono::system_clock::now(); + + std::time_t t = std::chrono::system_clock::to_time_t(point); + std::tm tm = *std::localtime(&t); + char strftime_output[256]; + std::strftime(strftime_output, sizeof(strftime_output), "It is %Y-%m-%d %H:%M:%S", &tm); + + EXPECT_EQ(strftime_output, fmt::format("It is {:%Y-%m-%d %H:%M:%S}", point)); +} + #define EXPECT_TIME(spec, time, duration) \ { \ std::locale loc("ja_JP.utf8"); \