Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding a default format for std::chrono::time_point<std::chrono::syst… #2345

Merged
merged 12 commits into from
Jun 11, 2021
18 changes: 7 additions & 11 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,24 +451,16 @@ FMT_END_DETAIL_NAMESPACE
template <typename Char, typename Duration>
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
Char> : formatter<std::tm, Char> {
FMT_CONSTEXPR FMT_INLINE void generate_default_spec(char) {
this->specs = {"%Y-%m-%d %H:%M:%S", 17};
}
FMT_CONSTEXPR FMT_INLINE void generate_default_spec(wchar_t) {
this->specs = {L"%Y-%m-%d %H:%M:%S", 17};
}

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;
while (end != ctx.end() && *end != '}') ++end;
if (end == it) {
generate_default_spec(typename ParseContext::char_type{});
} else {
if (end == it)
this->specs = {default_spec, 17};
sunmy2019 marked this conversation as resolved.
Show resolved Hide resolved
else
this->specs = {it, detail::to_unsigned(end - it)};
}
return end;
}

Expand All @@ -478,6 +470,10 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
std::tm time = localtime(val);
return formatter<std::tm, Char>::format(time, ctx);
}

static constexpr Char default_spec[] = {'%', 'Y', '-', '%', 'm', '-',
sunmy2019 marked this conversation as resolved.
Show resolved Hide resolved
'%', 'd', ' ', '%', 'H', ':',
'%', 'M', ':', '%', 'S'};
};

template <typename Char> struct formatter<std::tm, Char> {
Expand Down