adding a default format for std::chrono::time_point<std::chrono::system_clock, Duration>
This commit is contained in:
parent
d551b88a6d
commit
278929303e
@ -451,6 +451,27 @@ 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_defalut_spec(char) {
|
||||||
|
this->specs = "%Y-%m-%d %H:%M:%S";
|
||||||
|
}
|
||||||
|
FMT_CONSTEXPR FMT_INLINE void generate_defalut_spec(wchar_t) {
|
||||||
|
this->specs = L"%Y-%m-%d %H:%M:%S";
|
||||||
|
}
|
||||||
|
|
||||||
|
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_defalut_spec(typename ParseContext::char_type{});
|
||||||
|
} else {
|
||||||
|
this->specs = {it, detail::to_unsigned(end - it)};
|
||||||
|
}
|
||||||
|
return end;
|
||||||
|
}
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(std::chrono::time_point<std::chrono::system_clock> val,
|
auto format(std::chrono::time_point<std::chrono::system_clock> val,
|
||||||
FormatContext& ctx) -> decltype(ctx.out()) {
|
FormatContext& ctx) -> decltype(ctx.out()) {
|
||||||
|
|||||||
@ -99,6 +99,7 @@ template <typename TimePoint> auto strftime(TimePoint tp) -> std::string {
|
|||||||
TEST(chrono_test, time_point) {
|
TEST(chrono_test, time_point) {
|
||||||
auto t1 = std::chrono::system_clock::now();
|
auto t1 = std::chrono::system_clock::now();
|
||||||
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
|
EXPECT_EQ(strftime(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
|
||||||
|
EXPECT_EQ(strftime(t1), fmt::format("{}", t1));
|
||||||
using time_point =
|
using time_point =
|
||||||
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
|
||||||
auto t2 = time_point(std::chrono::seconds(42));
|
auto t2 = time_point(std::chrono::seconds(42));
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user