Fix fmt::localtime formatting not working in wide-char string contexts

This commit is contained in:
Yuval Gamzon-Kapeller 2021-02-07 10:19:17 +02:00 committed by Victor Zverovich
parent e6ef927e6b
commit c5979d564e
2 changed files with 20 additions and 7 deletions

View File

@ -413,7 +413,8 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock>, Char>
}; };
template <typename Char> struct formatter<std::tm, Char> { template <typename Char> struct formatter<std::tm, Char> {
FMT_CONSTEXPR auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) { template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(); auto it = ctx.begin();
if (it != ctx.end() && *it == ':') ++it; if (it != ctx.end() && *it == ':') ++it;
auto end = it; auto end = it;

View File

@ -95,17 +95,29 @@ TEST(TimeTest, GMTime) {
EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t)));
} }
TEST(TimeTest, TimePoint) { TEST(TimeTest, FormatTM) {
std::chrono::system_clock::time_point point = auto point = std::chrono::system_clock::now();
std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(point); std::time_t t = std::chrono::system_clock::to_time_t(point);
std::tm tm = *std::localtime(&t); std::tm tm = *std::localtime(&t);
char strftime_output[256]; char strftime_output[256];
std::strftime(strftime_output, sizeof(strftime_output), 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) \ #define EXPECT_TIME(spec, time, duration) \