Workaround a bug in MSVC's strftime (#965)

This commit is contained in:
Victor Zverovich 2018-12-07 07:07:21 -08:00
parent 628f830583
commit acfa95d4a8
2 changed files with 10 additions and 2 deletions

View File

@ -370,7 +370,7 @@ struct formatter<std::tm, Char> {
template <typename FormatContext>
auto format(const std::tm &tm, FormatContext &ctx) -> decltype(ctx.out()) {
internal::basic_buffer<Char> &buf = internal::get_container(ctx.out());
basic_memory_buffer<Char> buf;
std::size_t start = buf.size();
for (;;) {
std::size_t size = buf.capacity() - start;
@ -390,7 +390,7 @@ struct formatter<std::tm, Char> {
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<Char> tm_format;

View File

@ -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()));
}