aboutsummaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorThorsten Töpper <atsutane@freethoughts.de>2025-08-21 20:16:35 +0200
committerThorsten Töpper <atsutane@freethoughts.de>2025-08-21 20:16:35 +0200
commit0b3d9a2b4017ecde14e9464415fc19fe5b5e094c (patch)
treebe17760fd9c7914af5bf60f81cd0c3d16d550c02 /include
parent9db593de7f8de8245468109be09aaa2e7bad32ef (diff)
downloadsmall-utils-0b3d9a2b4017ecde14e9464415fc19fe5b5e094c.tar.gz
small-utils-0b3d9a2b4017ecde14e9464415fc19fe5b5e094c.tar.bz2
time_utils.h: difftime_timespec()
Diffstat (limited to 'include')
-rw-r--r--include/time_utils.h40
1 files changed, 40 insertions, 0 deletions
diff --git a/include/time_utils.h b/include/time_utils.h
new file mode 100644
index 0000000..f80e116
--- /dev/null
+++ b/include/time_utils.h
@@ -0,0 +1,40 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+/* Copyright 2025 Thorsten Töpper
+ * vim:ts=4:sw=4:expandtab
+ */
+#ifndef TIME_UTILS_H
+#define TIME_UTILS_H
+
+#include <time.h>
+
+#define TU_MEASURE_TIME(clck, ts1, ts2, code) \
+ clock_gettime(clck, ts1); \
+ code \
+ clock_gettime(clck, ts2);
+
+int difftime_timespec(struct timespec ts1, struct timespec ts2, struct timespec *result);
+
+
+inline int difftime_timespec(struct timespec ts1, struct timespec ts2, struct timespec *result) {
+ if (result == NULL)
+ return -1;
+ result->tv_sec = 0;
+ result->tv_nsec = 0;
+
+ if (ts1.tv_sec > ts2.tv_sec ||
+ ( (ts1.tv_sec == ts2.tv_sec) && ts1.tv_nsec > ts2.tv_nsec))
+ return -2;
+
+ result->tv_sec = ts2.tv_sec - ts1.tv_sec;
+
+ if (ts1.tv_nsec > ts2.tv_nsec) {
+ result->tv_nsec = (ts2.tv_nsec+1000000000) - ts1.tv_nsec;
+ } else {
+ result->tv_nsec = ts2.tv_nsec - ts1.tv_nsec;
+ }
+ return 0;
+}
+
+#endif
+