diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 3040b92cb96a7..82e0edce2bf59 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -553,6 +553,9 @@ template struct formatter { }; spec spec_ = spec::unknown; + static constexpr Char f_specs[] = {'%', 'F'}; + static constexpr Char t_specs[] = {'%', 'T'}; + public: basic_string_view specs; @@ -564,9 +567,11 @@ 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)) + if (specs == + basic_string_view(f_specs, sizeof(f_specs) / sizeof(Char))) spec_ = spec::year_month_day; - else if (specs == string_view("%T", 2)) + else if (specs == + basic_string_view(t_specs, sizeof(t_specs) / sizeof(Char))) 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) {