diff options
| -rw-r--r-- | include/time_utils.h | 40 |
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 + |
