diff --git a/AK/Time.cpp b/AK/Time.cpp index 83fe3f7d96..e09df2451c 100644 --- a/AK/Time.cpp +++ b/AK/Time.cpp @@ -44,6 +44,7 @@ Time Time::from_timespec(const struct timespec& ts) i32 extra_secs = sane_mod(nsecs, 1'000'000'000); return Time::from_half_sanitized(ts.tv_sec, extra_secs, nsecs); } + Time Time::from_timeval(const struct timeval& tv) { i32 usecs = tv.tv_usec; @@ -61,6 +62,7 @@ i64 Time::to_truncated_seconds() const } return m_seconds; } + i64 Time::to_truncated_milliseconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -79,6 +81,7 @@ i64 Time::to_truncated_milliseconds() const return milliseconds.value(); return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL; } + i64 Time::to_truncated_microseconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -97,6 +100,7 @@ i64 Time::to_truncated_microseconds() const return microseconds.value(); return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL; } + i64 Time::to_seconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -107,6 +111,7 @@ i64 Time::to_seconds() const } return m_seconds; } + i64 Time::to_milliseconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -123,6 +128,7 @@ i64 Time::to_milliseconds() const return milliseconds.value(); return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL; } + i64 Time::to_microseconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -139,6 +145,7 @@ i64 Time::to_microseconds() const return microseconds.value(); return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL; } + i64 Time::to_nanoseconds() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -153,11 +160,13 @@ i64 Time::to_nanoseconds() const return nanoseconds.value(); return m_seconds < 0 ? -0x8000'0000'0000'0000LL : 0x7fff'ffff'ffff'ffffLL; } + timespec Time::to_timespec() const { VERIFY(m_nanoseconds < 1'000'000'000); return { static_cast(m_seconds), static_cast(m_nanoseconds) }; } + timeval Time::to_timeval() const { VERIFY(m_nanoseconds < 1'000'000'000); @@ -239,14 +248,17 @@ bool Time::operator<(const Time& other) const { return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds < other.m_nanoseconds); } + bool Time::operator<=(const Time& other) const { return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds <= other.m_nanoseconds); } + bool Time::operator>(const Time& other) const { return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds > other.m_nanoseconds); } + bool Time::operator>=(const Time& other) const { return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds >= other.m_nanoseconds); @@ -283,6 +295,7 @@ static Time now_time_from_clock(clockid_t clock_id) return Time::from_timespec(now_spec); } } + Time Time::now_realtime() { return now_time_from_clock(CLOCK_REALTIME);