From e8ce0f0eba162ccd6da41fc61e286a1b1527fd82 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Mon, 12 Sep 2022 13:26:02 +0200 Subject: [PATCH] LibCore: Make DateTime more easily debuggable In particular, implement operator== and AK::Formatter. --- Userland/Libraries/LibCore/DateTime.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Userland/Libraries/LibCore/DateTime.h b/Userland/Libraries/LibCore/DateTime.h index e605d1f1d9..3c8879034f 100644 --- a/Userland/Libraries/LibCore/DateTime.h +++ b/Userland/Libraries/LibCore/DateTime.h @@ -39,6 +39,7 @@ public: static Optional parse(StringView format, DeprecatedString const& string); bool operator<(DateTime const& other) const { return m_timestamp < other.m_timestamp; } + bool operator==(DateTime const& other) const { return m_timestamp == other.m_timestamp; } private: time_t m_timestamp { 0 }; @@ -52,6 +53,19 @@ private: } +namespace AK { +template<> +struct Formatter : StandardFormatter { + ErrorOr format(FormatBuilder& builder, Core::DateTime const& value) + { + // Can't use DateTime::to_string() here: It doesn't propagate allocation failure. + return builder.builder().try_appendff("{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{:02d}"sv, + value.year(), value.month(), value.day(), + value.hour(), value.minute(), value.second()); + } +}; +} + namespace IPC { template<>