/* 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 #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