diff --git a/AK/Time.h b/AK/Time.h index e29229d19e..dd3e48c770 100644 --- a/AK/Time.h +++ b/AK/Time.h @@ -61,6 +61,17 @@ inline void timespec_sub(const TimespecType& a, const TimespecType& b, TimespecT } } +template +inline void timespec_sub_timeval(const TimespecType& a, const TimevalType& b, TimespecType& result) +{ + result.tv_sec = a.tv_sec - b.tv_sec; + result.tv_nsec = a.tv_nsec - b.tv_usec * 1000; + if (result.tv_nsec < 0) { + --result.tv_sec; + result.tv_nsec += 1'000'000'000; + } +} + template inline void timespec_add(const TimespecType& a, const TimespecType& b, TimespecType& result) { @@ -72,6 +83,17 @@ inline void timespec_add(const TimespecType& a, const TimespecType& b, TimespecT } } +template +inline void timespec_add_timeval(const TimespecType& a, const TimevalType& b, TimespecType& result) +{ + result.tv_sec = a.tv_sec + b.tv_sec; + result.tv_nsec = a.tv_nsec + b.tv_usec * 1000; + if (result.tv_nsec >= 1000'000'000) { + ++result.tv_sec; + result.tv_nsec -= 1000'000'000; + } +} + template inline void timeval_to_timespec(const TimevalType& tv, TimespecType& ts) { @@ -86,11 +108,55 @@ inline void timespec_to_timeval(const TimespecType& ts, TimevalType& tv) tv.tv_usec = ts.tv_nsec / 1000; } +template +inline bool operator>=(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec >= b.tv_nsec); +} + +template +inline bool operator>(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec); +} + +template +inline bool operator<(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec); +} + +template +inline bool operator<=(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec); +} + +template +inline bool operator==(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec; +} + +template +inline bool operator!=(const TimespecType& a, const TimespecType& b) +{ + return a.tv_sec != b.tv_sec || a.tv_nsec != b.tv_nsec; +} + } using AK::timespec_add; +using AK::timespec_add_timeval; using AK::timespec_sub; +using AK::timespec_sub_timeval; using AK::timespec_to_timeval; using AK::timeval_add; using AK::timeval_sub; using AK::timeval_to_timespec; +using AK::operator<=; +using AK::operator<; +using AK::operator>; +using AK::operator>=; +using AK::operator==; +using AK::operator!=;