mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Add timespec_add and timespec_sub
This commit is contained in:
parent
9e18005c64
commit
9256757b59
Notes:
sideshowbarker
2024-07-19 05:27:30 +09:00
Author: https://github.com/nico Commit: https://github.com/SerenityOS/serenity/commit/9256757b591 Pull-request: https://github.com/SerenityOS/serenity/pull/2609 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/bugaevc
1 changed files with 28 additions and 4 deletions
32
AK/Time.h
32
AK/Time.h
|
@ -35,7 +35,7 @@ inline void timeval_sub(const TimevalType& a, const TimevalType& b, TimevalType&
|
|||
result.tv_usec = a.tv_usec - b.tv_usec;
|
||||
if (result.tv_usec < 0) {
|
||||
--result.tv_sec;
|
||||
result.tv_usec += 1000000;
|
||||
result.tv_usec += 1'000'000;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,31 @@ inline void timeval_add(const TimevalType& a, const TimevalType& b, TimevalType&
|
|||
{
|
||||
result.tv_sec = a.tv_sec + b.tv_sec;
|
||||
result.tv_usec = a.tv_usec + b.tv_usec;
|
||||
if (result.tv_usec > 1000000) {
|
||||
if (result.tv_usec > 1'000'000) {
|
||||
++result.tv_sec;
|
||||
result.tv_usec -= 1000000;
|
||||
result.tv_usec -= 1'000'000;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline void timespec_sub(const TimespecType& a, const TimespecType& b, TimespecType& result)
|
||||
{
|
||||
result.tv_sec = a.tv_sec - b.tv_sec;
|
||||
result.tv_nsec = a.tv_nsec - b.tv_nsec;
|
||||
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)
|
||||
{
|
||||
result.tv_sec = a.tv_sec + b.tv_sec;
|
||||
result.tv_nsec = a.tv_nsec + b.tv_nsec;
|
||||
if (result.tv_nsec > 1000'000'000) {
|
||||
++result.tv_sec;
|
||||
result.tv_nsec -= 1000'000'000;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -66,7 +88,9 @@ inline void timespec_to_timeval(const TimespecType& ts, TimevalType& tv)
|
|||
|
||||
}
|
||||
|
||||
using AK::timespec_add;
|
||||
using AK::timespec_sub;
|
||||
using AK::timespec_to_timeval;
|
||||
using AK::timeval_add;
|
||||
using AK::timeval_sub;
|
||||
using AK::timeval_to_timespec;
|
||||
using AK::timespec_to_timeval;
|
||||
|
|
Loading…
Reference in a new issue