diff options
Diffstat (limited to 'source/time.c')
| -rw-r--r-- | source/time.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/source/time.c b/source/time.c new file mode 100644 index 0000000..e2c47b0 --- /dev/null +++ b/source/time.c @@ -0,0 +1,56 @@ +#include "time.h" + +struct timespec timespec_add(struct timespec a, struct timespec b) { + a.tv_sec += b.tv_sec; + a.tv_nsec += b.tv_nsec; + if (a.tv_nsec >= 1000000000) { + a.tv_sec++; + a.tv_nsec -= 1000000000; + } + return a; +} + +struct timespec timespec_sub(struct timespec a, struct timespec b) { + a.tv_sec -= b.tv_sec; + a.tv_nsec -= b.tv_nsec; + if (a.tv_nsec < 0) { + a.tv_sec--; + a.tv_nsec += 1000000000; + } + return a; +} + +int timespec_cmp(struct timespec a, struct timespec b) { + return a.tv_sec > b.tv_sec ? + (1) : + a.tv_sec < b.tv_sec ? + (-1) : + ( + a.tv_nsec > b.tv_nsec ? + (1) : + a.tv_nsec < b.tv_nsec ? + (-1) : 0 + ); +} + +struct timespec timespec_max(struct timespec a, struct timespec b) { + return a.tv_sec > b.tv_sec ? + a : + a.tv_sec < b.tv_sec ? + b : + ( + a.tv_nsec > b.tv_nsec ? + a : b + ); +} + +struct timespec timespec_min(struct timespec a, struct timespec b) { + return a.tv_sec < b.tv_sec ? + a : + a.tv_sec > b.tv_sec ? + b : + ( + a.tv_nsec < b.tv_nsec ? + a : b + ); +} |
