From a9113e58437c9d4954a7172764eff2a07c452ce0 Mon Sep 17 00:00:00 2001 From: Vladislav Shchapov Date: Mon, 13 Sep 2021 12:12:36 +0500 Subject: [PATCH] Fix wchar_t tm formatting --- include/fmt/chrono.h | 11 +++++++---- test/xchar-test.cc | 2 ++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 3040b92cb96a7..fc25311c8a722 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -564,10 +564,13 @@ template struct formatter { while (end != ctx.end() && *end != '}') ++end; auto size = detail::to_unsigned(end - it); specs = {it, size}; - if (specs == string_view("%F", 2)) - spec_ = spec::year_month_day; - else if (specs == string_view("%T", 2)) - spec_ = spec::hh_mm_ss; + // basic_string_view<>::compare isn't constexpr before C++17 + if (specs.size() == 2 && specs[0] == Char('%')) { + if (specs[1] == Char('F')) + spec_ = spec::year_month_day; + else if (specs[1] == Char('T')) + spec_ = spec::hh_mm_ss; + } return end; } diff --git a/test/xchar-test.cc b/test/xchar-test.cc index ca11d7a7632ec..751aeba75f891 100644 --- a/test/xchar-test.cc +++ b/test/xchar-test.cc @@ -261,6 +261,8 @@ TEST(xchar_test, chrono) { EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm), "The date is 2016-04-25 11:22:33."); EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42))); + EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25"); + EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33"); } TEST(xchar_test, color) {