From d051fffe25ba76c8a62b00289cf0f86e37a83f58 Mon Sep 17 00:00:00 2001 From: AnotherTest Date: Thu, 7 May 2020 09:11:07 +0430 Subject: [PATCH] LibCore: Add a primitive comparison function to DateTime This should go away when we get the ability to parse strings to DateTime's. --- Libraries/LibCore/DateTime.cpp | 6 ++++++ Libraries/LibCore/DateTime.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/Libraries/LibCore/DateTime.cpp b/Libraries/LibCore/DateTime.cpp index e6011951eaf..2b6704f1e87 100644 --- a/Libraries/LibCore/DateTime.cpp +++ b/Libraries/LibCore/DateTime.cpp @@ -280,6 +280,12 @@ String DateTime::to_string(const String& format) const return builder.build(); } +bool DateTime::is_before(const String& other) const +{ + auto now_string = String::format("%04d%02d%02d%02d%02d%02dZ", year(), month(), weekday(), hour(), minute(), second()); + return __builtin_strcasecmp(now_string.characters(), other.characters()) < 0; +} + const LogStream& operator<<(const LogStream& stream, const DateTime& value) { return stream << value.to_string(); diff --git a/Libraries/LibCore/DateTime.h b/Libraries/LibCore/DateTime.h index 4409b42b07b..f1f1f833edd 100644 --- a/Libraries/LibCore/DateTime.h +++ b/Libraries/LibCore/DateTime.h @@ -54,6 +54,10 @@ public: static DateTime now(); static DateTime from_timestamp(time_t); + // FIXME: This should be replaced with a proper comparison + // operator when we get the equivalent of strptime + bool is_before(const String&) const; + private: time_t m_timestamp { 0 }; unsigned m_year { 0 };