From a8ec2e15a568e237773f49dcec4b63564abb0d34 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sat, 21 Jan 2023 00:28:30 +0000 Subject: [PATCH] timeutils: add utilities for usec_t conversions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Thomas Weißschuh --- include/timeutils.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/timeutils.h b/include/timeutils.h index 1b7f4263a3..eb0e534510 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -92,4 +92,23 @@ int strtime_short(const time_t *t, struct timeval *now, int flags, char *buf, si extern time_t timegm(struct tm *tm); #endif +static inline usec_t timeval_to_usec(const struct timeval *t) +{ + return t->tv_sec * USEC_PER_SEC + t->tv_usec; +} + +static inline usec_t timespec_to_usec(const struct timespec *t) +{ + return t->tv_sec * USEC_PER_SEC + t->tv_nsec / NSEC_PER_USEC; +} + +static inline struct timeval usec_to_timeval(usec_t t) +{ + struct timeval r = { + .tv_sec = t / USEC_PER_SEC, + .tv_usec = t % USEC_PER_SEC, + }; + return r; +} + #endif /* UTIL_LINUX_TIME_UTIL_H */ -- 2.47.2