diff options
Diffstat (limited to 'libc/bionic/bionic_time_conversions.cpp')
| -rw-r--r-- | libc/bionic/bionic_time_conversions.cpp | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/libc/bionic/bionic_time_conversions.cpp b/libc/bionic/bionic_time_conversions.cpp index 7f3c026..75e8d49 100644 --- a/libc/bionic/bionic_time_conversions.cpp +++ b/libc/bionic/bionic_time_conversions.cpp @@ -28,6 +28,8 @@ #include "private/bionic_time_conversions.h" +#include "private/bionic_constants.h" + bool timespec_from_timeval(timespec& ts, const timeval& tv) { // Whole seconds can just be copied. ts.tv_sec = tv.tv_sec; @@ -49,3 +51,19 @@ void timeval_from_timespec(timeval& tv, const timespec& ts) { tv.tv_sec = ts.tv_sec; tv.tv_usec = ts.tv_nsec / 1000; } + +// Initializes 'ts' with the difference between 'abs_ts' and the current time +// according to 'clock'. Returns false if abstime already expired, true otherwise. +bool timespec_from_absolute_timespec(timespec& ts, const timespec& abs_ts, clockid_t clock) { + clock_gettime(clock, &ts); + ts.tv_sec = abs_ts.tv_sec - ts.tv_sec; + ts.tv_nsec = abs_ts.tv_nsec - ts.tv_nsec; + if (ts.tv_nsec < 0) { + ts.tv_sec--; + ts.tv_nsec += NS_PER_S; + } + if (ts.tv_nsec < 0 || ts.tv_sec < 0) { + return false; + } + return true; +} |
