From c5979d564e1ec40c383eaca610c8aa7cca1733a4 Mon Sep 17 00:00:00 2001 From: Yuval Gamzon-Kapeller Date: Sun, 7 Feb 2021 10:19:17 +0200 Subject: [PATCH] Fix fmt::localtime formatting not working in wide-char string contexts --- include/fmt/chrono.h | 3 ++- test/chrono-test.cc | 24 ++++++++++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 4b9aeeb1..1b71131d 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -413,7 +413,8 @@ struct formatter, Char> }; template struct formatter { - FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { + template + FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { auto it = ctx.begin(); if (it != ctx.end() && *it == ':') ++it; auto end = it; diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 6c4ff6da..e56b2bfe 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -95,17 +95,29 @@ 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(); - +TEST(TimeTest, FormatTM) { + auto 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); + "%Y-%m-%d %H:%M:%S", &tm); + EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", tm)); + auto wstrftime_output = std::wstring(); + std::copy(strftime_output, strftime_output + strlen(strftime_output), + std::back_inserter(wstrftime_output)); + EXPECT_EQ(wstrftime_output, + fmt::format(L"{:%Y-%m-%d %H:%M:%S}", tm)); +} - EXPECT_EQ(strftime_output, fmt::format("It is {:%Y-%m-%d %H:%M:%S}", point)); +TEST(TimeTest, TimePoint) { + auto 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), + "%Y-%m-%d %H:%M:%S", &tm); + EXPECT_EQ(strftime_output, fmt::format("{:%Y-%m-%d %H:%M:%S}", point)); } #define EXPECT_TIME(spec, time, duration) \