mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
AK: Add more time convenience functions and comparison operators
This commit is contained in:
parent
7d12e0c7f1
commit
df52061cdb
1 changed files with 66 additions and 0 deletions
66
AK/Time.h
66
AK/Time.h
|
@ -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!=;
|
||||
|
|
Loading…
Add table
Reference in a new issue