diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 5e03149d..11b9999e 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -451,6 +451,27 @@ FMT_END_DETAIL_NAMESPACE template struct formatter, Char> : formatter { + FMT_CONSTEXPR FMT_INLINE void generate_defalut_spec(char) { + this->specs = "%Y-%m-%d %H:%M:%S"; + } + FMT_CONSTEXPR FMT_INLINE void generate_defalut_spec(wchar_t) { + this->specs = L"%Y-%m-%d %H:%M:%S"; + } + + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { + auto it = ctx.begin(); + if (it != ctx.end() && *it == ':') ++it; + auto end = it; + while (end != ctx.end() && *end != '}') ++end; + if (end == it) { + generate_defalut_spec(typename ParseContext::char_type{}); + } else { + this->specs = {it, detail::to_unsigned(end - it)}; + } + return end; + } + template auto format(std::chrono::time_point val, FormatContext& ctx) -> decltype(ctx.out()) { diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 6bc2cdb0..cbcc3ec3 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -99,6 +99,7 @@ template auto strftime(TimePoint tp) -> std::string { TEST(chrono_test, time_point) { auto t1 = std::chrono::system_clock::now(); EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1)); + EXPECT_EQ(strftime(t1), fmt::format("{}", t1)); using time_point = std::chrono::time_point; auto t2 = time_point(std::chrono::seconds(42));