diff --git a/include/fmt/time.h b/include/fmt/time.h index 5013914f..a3f1e6b2 100644 --- a/include/fmt/time.h +++ b/include/fmt/time.h @@ -370,7 +370,7 @@ struct formatter { template auto format(const std::tm &tm, FormatContext &ctx) -> decltype(ctx.out()) { - internal::basic_buffer &buf = internal::get_container(ctx.out()); + basic_memory_buffer buf; std::size_t start = buf.size(); for (;;) { std::size_t size = buf.capacity() - start; @@ -390,7 +390,7 @@ struct formatter { const std::size_t MIN_GROWTH = 10; buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH)); } - return ctx.out(); + return std::copy(buf.begin(), buf.end(), ctx.out()); } basic_memory_buffer tm_format; diff --git a/test/time-test.cc b/test/time-test.cc index b6fcc401..52be071a 100644 --- a/test/time-test.cc +++ b/test/time-test.cc @@ -31,6 +31,14 @@ TEST(TimeTest, GrowBuffer) { fmt::format(s, *std::localtime(&t)); } +TEST(TimeTest, FormatToEmptyContainer) { + std::string s; + auto time = std::tm(); + time.tm_sec = 42; + fmt::format_to(std::back_inserter(s), "{:%S}", time); + EXPECT_EQ(s, "42"); +} + TEST(TimeTest, EmptyResult) { EXPECT_EQ("", fmt::format("{}", std::tm())); }