AK: Add more time convenience functions and comparison operators

This commit is contained in:
Tom 2020-08-03 09:42:39 -06:00 committed by Andreas Kling
parent 7d12e0c7f1
commit df52061cdb

View file

@ -61,6 +61,17 @@ inline void timespec_sub(const TimespecType& a, const TimespecType& b, TimespecT
}
}
template<typename TimespecType, typename TimevalType>
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<typename TimespecType>
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<typename TimespecType, typename TimevalType>
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<typename TimevalType, typename TimespecType>
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<typename TimespecType>
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<typename TimespecType>
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<typename TimespecType>
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<typename TimespecType>
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<typename TimespecType>
inline bool operator==(const TimespecType& a, const TimespecType& b)
{
return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
}
template<typename TimespecType>
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!=;