From 6db4e40119dd7c3b5fed3018a81e425e822929e9 Mon Sep 17 00:00:00 2001 From: ejurgensen Date: Sun, 15 Jan 2017 23:19:57 +0100 Subject: [PATCH] [misc] Add function to add a relative time to current clock --- src/misc.c | 16 ++++++++++++++++ src/misc.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/misc.c b/src/misc.c index f624bd4a..769b15e2 100644 --- a/src/misc.c +++ b/src/misc.c @@ -27,6 +27,7 @@ #endif #include +#include #include #include #include @@ -928,6 +929,21 @@ timespec_cmp(struct timespec time1, struct timespec time2) return 0; } +struct timespec +timespec_reltoabs(struct timespec relative) +{ + struct timespec absolute; + +#if _POSIX_TIMERS > 0 + clock_gettime(CLOCK_REALTIME, &absolute); +#else + struct timeval tv; + gettimeofday(&tv, NULL); + TIMEVAL_TO_TIMESPEC(&tv, &absolute); +#endif + return timespec_add(absolute, relative); +} + #if defined(HAVE_MACH_CLOCK) || defined(HAVE_MACH_TIMER) #include /* mach_absolute_time */ diff --git a/src/misc.h b/src/misc.h index c358dde9..de1a089e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -145,6 +145,9 @@ timespec_add(struct timespec time1, struct timespec time2); int timespec_cmp(struct timespec time1, struct timespec time2); +struct timespec +timespec_reltoabs(struct timespec relative); + /* initialize mutex with error checking (not default on all platforms) */ int mutex_init(pthread_mutex_t *mutex);