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> {
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;

View File

@ -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) \