]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
timeutils: add utilities for usec_t conversions
authorThomas Weißschuh <thomas@t-8ch.de>
Sat, 21 Jan 2023 00:28:30 +0000 (00:28 +0000)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 24 Jan 2023 16:04:45 +0000 (16:04 +0000)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
include/timeutils.h

index 1b7f4263a34e7372d8a6eb33098761a2be36fe70..eb0e534510ba5b8f1e73594da1d6d420cd48b4e8 100644 (file)
@@ -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 */