using static constexpr char array as default format

This commit is contained in:
sunmy 2021-06-09 21:49:07 +08:00
parent c6f8112a06
commit aab4a1bfa2

View File

@ -451,24 +451,16 @@ FMT_END_DETAIL_NAMESPACE
template <typename Char, typename Duration> template <typename Char, typename Duration>
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>, struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
Char> : formatter<std::tm, Char> { 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> template <typename ParseContext>
FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) { FMT_CONSTEXPR auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(); auto it = ctx.begin();
if (it != ctx.end() && *it == ':') ++it; if (it != ctx.end() && *it == ':') ++it;
auto end = it; auto end = it;
while (end != ctx.end() && *end != '}') ++end; while (end != ctx.end() && *end != '}') ++end;
if (end == it) { if (end == it)
generate_default_spec(typename ParseContext::char_type{}); this->specs = {default_spec, 17};
} else { else
this->specs = {it, detail::to_unsigned(end - it)}; this->specs = {it, detail::to_unsigned(end - it)};
}
return end; return end;
} }
@ -478,6 +470,10 @@ struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>,
std::tm time = localtime(val); std::tm time = localtime(val);
return formatter<std::tm, Char>::format(time, ctx); return formatter<std::tm, Char>::format(time, ctx);
} }
static constexpr Char default_spec[] = {'%', 'Y', '-', '%', 'm', '-',
'%', 'd', ' ', '%', 'H', ':',
'%', 'M', ':', '%', 'S'};
}; };
template <typename Char> struct formatter<std::tm, Char> { template <typename Char> struct formatter<std::tm, Char> {