Skip to content

Commit

Permalink
Fix wchar_t tm formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
phprus committed Sep 13, 2021
1 parent 140727c commit 6c6239d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
9 changes: 7 additions & 2 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,9 @@ template <typename Char> struct formatter<std::tm, Char> {
};
spec spec_ = spec::unknown;

static constexpr Char f_specs[] = {'%', 'F'};
static constexpr Char t_specs[] = {'%', 'T'};

public:
basic_string_view<Char> specs;

Expand All @@ -564,9 +567,11 @@ template <typename Char> struct formatter<std::tm, Char> {
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<Char>(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<Char>(t_specs, sizeof(t_specs) / sizeof(Char)))
spec_ = spec::hh_mm_ss;
return end;
}
Expand Down
2 changes: 2 additions & 0 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 6c6239d

Please sign in to comment.