]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/uktimeconv: Add conversion functions
authorAndrei Tatar <andrei@unikraft.io>
Thu, 16 Nov 2023 18:22:22 +0000 (19:22 +0100)
committerSimon Kuenzer <simon@unikraft.io>
Tue, 28 Nov 2023 17:10:11 +0000 (18:10 +0100)
This change adds a header with utility functions related to various time
formats defined by standard C or POSIX.
This header depends on time headers provided by (no)libc.

Signed-off-by: Andrei Tatar <andrei@unikraft.io>
Reviewed-by: Simon Kuenzer <simon@unikraft.io>
Approved-by: Simon Kuenzer <simon@unikraft.io>
GitHub-Closes: #1162

lib/uktimeconv/Config.uk
lib/uktimeconv/include/uk/timeutil.h [new file with mode: 0644]

index f0494ca38095d4d7766b5a909d979567e7d1f9d8..ab954102f4720aa1263be58ea1c764d181f8fe77 100644 (file)
@@ -2,3 +2,4 @@ config LIBUKTIMECONV
        bool "uktimeconv: Time conversion functions"
        default n
        select LIBUKDEBUG
+       select LIBNOLIBC if !HAVE_LIBC
diff --git a/lib/uktimeconv/include/uk/timeutil.h b/lib/uktimeconv/include/uk/timeutil.h
new file mode 100644 (file)
index 0000000..2f5cf34
--- /dev/null
@@ -0,0 +1,65 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Copyright (c) 2023, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the BSD-3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ */
+
+#ifndef __UK_TIMEUTIL_H__
+#define __UK_TIMEUTIL_H__
+
+#include <sys/time.h>
+#include <time.h>
+
+#include <uk/arch/time.h>
+
+
+#define uk_time_spec_to_nsec(t) \
+       (ukarch_time_sec_to_nsec((t)->tv_sec) + (t)->tv_nsec)
+
+#define uk_time_val_to_nsec(tv) ( \
+       ukarch_time_sec_to_nsec((tv)->tv_sec) + \
+       ukarch_time_usec_to_nsec((tv)->tv_usec) \
+)
+
+#define uk_time_spec_from_nsec(ns) ((struct timespec){ \
+       .tv_sec = ukarch_time_nsec_to_sec((ns)), \
+       .tv_nsec = ukarch_time_subsec((ns)) \
+})
+
+#define uk_time_spec_from_msec(ms) \
+       uk_time_spec_from_nsec(ukarch_time_msec_to_nsec((ms)))
+
+#define uk_time_spec_from_val(tv) ((struct timespec){ \
+       .tv_sec = (tv)->tv_sec, \
+       .tv_nsec = ukarch_time_usec_to_nsec((tv)->tv_usec) \
+})
+
+#define uk_time_val_from_spec(ts) ((struct timeval){ \
+       .tv_sec = (ts)->tv_sec, \
+       .tv_usec = ukarch_time_nsec_to_usec((ts)->tv_nsec) \
+})
+
+static inline
+__snsec uk_time_spec_nsecdiff(const struct timespec *t1,
+                             const struct timespec *t2)
+{
+       return (t2->tv_sec - t1->tv_sec) * UKARCH_NSEC_PER_SEC +
+              t2->tv_nsec - t1->tv_nsec;
+}
+
+static inline
+struct timespec uk_time_spec_sum(const struct timespec *t1,
+                                const struct timespec *t2)
+{
+       struct timespec ret;
+
+       ret.tv_sec = t1->tv_sec + t2->tv_sec;
+       ret.tv_nsec = t1->tv_nsec + t2->tv_nsec;
+       if (ret.tv_nsec > (long)UKARCH_NSEC_PER_SEC) {
+               ret.tv_sec += 1;
+               ret.tv_nsec -= UKARCH_NSEC_PER_SEC;
+       }
+       return ret;
+}
+
+#endif /*__UK_TIMEUTIL_H__ */