diff --git a/AK/Time.cpp b/AK/Time.cpp index 6eb9d2bdedb..f91dd358dd1 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -256,26 +256,6 @@ Time& Time::operator-=(Time const& other) return *this; } -bool Time::operator<(Time const& other) const -{ - return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds < other.m_nanoseconds); -} - -bool Time::operator<=(Time const& other) const -{ - return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds <= other.m_nanoseconds); -} - -bool Time::operator>(Time const& other) const -{ - return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds > other.m_nanoseconds); -} - -bool Time::operator>=(Time const& other) const -{ - return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds >= other.m_nanoseconds); -} - Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds) { VERIFY(nanoseconds < 1'000'000'000); diff --git a/AK/Time.h b/AK/Time.h index 526e55f7630..0b13b4a48ed 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -254,15 +254,24 @@ public: [[nodiscard]] bool is_zero() const { return (m_seconds == 0) && (m_nanoseconds == 0); } [[nodiscard]] bool is_negative() const { return m_seconds < 0; } - bool operator==(Time const& other) const { return this->m_seconds == other.m_seconds && this->m_nanoseconds == other.m_nanoseconds; } Time operator+(Time const& other) const; Time& operator+=(Time const& other); Time operator-(Time const& other) const; Time& operator-=(Time const& other); - bool operator<(Time const& other) const; - bool operator<=(Time const& other) const; - bool operator>(Time const& other) const; - bool operator>=(Time const& other) const; + + constexpr bool operator==(Time const& other) const = default; + constexpr int operator<=>(Time const& other) const + { + if (int cmp = (m_seconds > other.m_seconds ? 1 : m_seconds < other.m_seconds ? -1 + : 0); + cmp != 0) + return cmp; + if (int cmp = (m_nanoseconds > other.m_nanoseconds ? 1 : m_nanoseconds < other.m_nanoseconds ? -1 + : 0); + cmp != 0) + return cmp; + return 0; + } private: constexpr explicit Time(i64 seconds, u32 nanoseconds) diff --git a/Tests/AK/TestTime.cpp b/Tests/AK/TestTime.cpp index caafbdab95f..00df3057019 100644 --- a/Tests/AK/TestTime.cpp +++ b/Tests/AK/TestTime.cpp @@ -475,3 +475,18 @@ TEST_CASE(years_to_days_since_epoch_span) EXPECT_EQ(actual_days, expected_days); } } + +TEST_CASE(user_defined_literals) +{ + using namespace AK::TimeLiterals; + static_assert(Time::from_nanoseconds(123) == 123_ns, "Factory is same as UDL"); + + static_assert(100_ms > 10_ms, "LT UDL"); + static_assert(1000_ns == 1_us, "EQ UDL"); + static_assert(1_sec > 1_ms, "GT UDL"); + static_assert(100_ms >= 100'000_us, "GE UDL (eq)"); + static_assert(100_ms >= 99'999_us, "GE UDL (gt)"); + static_assert(100_ms <= 100'000_us, "LE UDL (eq)"); + static_assert(100_ms <= 100'001_us, "LE UDL (lt)"); + static_assert(1_sec != 2_sec, "NE UDL"); +}