diff options
author | Tudor Brindus <me@tbrindus.ca> | 2020-06-05 17:56:13 -0400 |
---|---|---|
committer | Simon Ser <contact@emersion.fr> | 2020-06-06 00:09:19 +0200 |
commit | 363bf44a353ca0ce7e9d48476fb187d8da037483 (patch) | |
tree | a61b989a365a8a5f425eb18c995241184382cbaf /util/time.c | |
parent | c9c31f803eaebf69481d24ac9355cd1f8d3111d9 (diff) |
util/time: move `timespec_sub` to time utilities
Diffstat (limited to 'util/time.c')
-rw-r--r-- | util/time.c | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/util/time.c b/util/time.c index a47fc613..aed577de 100644 --- a/util/time.c +++ b/util/time.c @@ -4,6 +4,8 @@ #include "util/time.h" +const long NSEC_PER_SEC = 1000000000; + int64_t timespec_to_msec(const struct timespec *a) { return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; } @@ -13,3 +15,13 @@ uint32_t get_current_time_msec(void) { clock_gettime(CLOCK_MONOTONIC, &now); return timespec_to_msec(&now); } + +void timespec_sub(struct timespec *r, const struct timespec *a, + const struct timespec *b) { + r->tv_sec = a->tv_sec - b->tv_sec; + r->tv_nsec = a->tv_nsec - b->tv_nsec; + if (r->tv_nsec < 0) { + r->tv_sec--; + r->tv_nsec += NSEC_PER_SEC; + } +} |