Skip to content

Commit

Permalink
Fix fmt::localtime formatting not working in wide-char string contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
yNeo authored and vitaut committed Feb 13, 2021
1 parent e6ef927 commit c5979d5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 2 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock>, 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();
if (it != ctx.end() && *it == ':') ++it;
auto end = it;
Expand Down
24 changes: 18 additions & 6 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down

0 comments on commit c5979d5

Please sign in to comment.