[misc] Add function to add a relative time to current clock

This commit is contained in:
ejurgensen 2017-01-15 23:19:57 +01:00
parent 9aede45a12
commit 6db4e40119
2 changed files with 19 additions and 0 deletions

View File

@ -27,6 +27,7 @@
#endif
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
@ -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/mach_time.h> /* mach_absolute_time */

View File

@ -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);