From 1e53e5e45e442d4d1dee62c736e28f29a0656834 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 26 Apr 2020 13:32:08 +0000 Subject: [PATCH] timeutil: Add timespec conversion helpers --- include/time-util.h | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/include/time-util.h b/include/time-util.h index 7eecf8b..5653d2c 100644 --- a/include/time-util.h +++ b/include/time-util.h @@ -19,16 +19,28 @@ #include #include +static inline uint64_t timespec_to_us(const struct timespec* ts) +{ + return (uint64_t)ts->tv_sec * UINT64_C(1000000) + + (uint64_t)ts->tv_nsec / UINT64_C(1000); +} + +static inline uint64_t timespec_to_ms(const struct timespec* ts) +{ + return (uint64_t)ts->tv_sec * UINT64_C(1000) + + (uint64_t)ts->tv_nsec / UINT64_C(1000000); +} + static inline uint64_t gettime_us(void) { struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000000ULL + (double)ts.tv_nsec / 1000ULL; + return timespec_to_us(&ts); } static inline uint32_t gettime_ms(void) { struct timespec ts = { 0 }; clock_gettime(CLOCK_MONOTONIC, &ts); - return ts.tv_sec * 1000UL + ts.tv_nsec / 1000000UL; + return timespec_to_ms(&ts); }