From 3fcd58c715993b3c2a42e21d41fece5d7d09243c Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 12 Aug 2019 17:59:10 +0300 Subject: time: add timeval versions. We now got functions to operate on both struct timespec and struct timeval. Signed-off-by: Gediminas Jakutis --- src/time/time.c | 104 ++++++++++++++++++++++++++++++++++++++++++++++++ src/time/time_private.h | 1 + 2 files changed, 105 insertions(+) (limited to 'src') diff --git a/src/time/time.c b/src/time/time.c index d9f00b7..489196d 100644 --- a/src/time/time.c +++ b/src/time/time.c @@ -62,6 +62,47 @@ struct timespec rin_time_normalize(const struct timespec * const t) return ret; } +struct timeval rin_timeval_add(const struct timeval * const a, const struct timeval * const b) +{ + struct timeval ret; + + ret.tv_sec = a->tv_sec + b->tv_sec; + ret.tv_usec = a->tv_usec + b->tv_usec; + + return rin_timeval_normalize(&ret); +} + +struct timeval rin_timeval_sub(const struct timeval * const a, const struct timeval * const b) +{ + struct timeval ret; + + ret.tv_sec = a->tv_sec - b->tv_sec; + ret.tv_usec = a->tv_usec - b->tv_usec; + + return rin_timeval_normalize(&ret); +} + +struct timeval rin_timeval_normalize(const struct timeval * const t) +{ + struct timeval ret; + + ret = *t; + + if (ret.tv_usec >= 0 && ret.tv_usec <= tv_usec_max) { + return ret; + } + + if (ret.tv_usec > 0) { + ret.tv_sec += ret.tv_usec / (tv_usec_max + 1); + ret.tv_usec = ret.tv_usec % (tv_usec_max + 1); + } else { + ret.tv_sec += ret.tv_usec / (tv_usec_max + 1) - 1; + ret.tv_usec = (tv_usec_max + 1) - (-ret.tv_usec) % (tv_usec_max + 1); + } + + return ret; +} + long int rin_time_cmp(const struct timespec * const a, const struct timespec * const b) { if (a->tv_sec != b->tv_sec) { @@ -71,6 +112,15 @@ long int rin_time_cmp(const struct timespec * const a, const struct timespec * c } } +long int rin_timeval_cmp(const struct timeval * const a, const struct timeval * const b) +{ + if (a->tv_sec != b->tv_sec) { + return a->tv_sec - b->tv_sec; + } else { + return a->tv_usec - b->tv_usec; + } +} + unsigned int rin_time_cmp_less(const struct timespec * const a, const struct timespec * const b) { if (a->tv_sec > b->tv_sec) { @@ -124,3 +174,57 @@ unsigned int rin_time_cmp_nonequal(const struct timespec * const a, const struct { return a->tv_sec != b->tv_sec || a->tv_nsec != b->tv_nsec; } + +unsigned int rin_timeval_cmp_less(const struct timeval * const a, const struct timeval * const b) +{ + if (a->tv_sec > b->tv_sec) { + return 0; + } else if (a->tv_sec < b->tv_sec) { + return 1; + } else { + return a->tv_usec < b->tv_usec; + } +} + +unsigned int rin_timeval_cmp_more(const struct timeval * const a, const struct timeval * const b) +{ + if (a->tv_sec < b->tv_sec) { + return 0; + } else if (a->tv_sec > b->tv_sec) { + return 1; + } else { + return a->tv_usec > b->tv_usec; + } +} + +unsigned int rin_timeval_cmp_lessequal(const struct timeval * const a, const struct timeval * const b) +{ + if (a->tv_sec > b->tv_sec) { + return 0; + } else if (a->tv_sec < b->tv_sec) { + return 1; + } else { + return a->tv_usec <= b->tv_usec; + } +} + +unsigned int rin_timeval_cmp_moreequal(const struct timeval * const a, const struct timeval * const b) +{ + if (a->tv_sec < b->tv_sec) { + return 0; + } else if (a->tv_sec > b->tv_sec) { + return 1; + } else { + return a->tv_usec >= b->tv_usec; + } +} + +unsigned int rin_timeval_cmp_equal(const struct timeval * const a, const struct timeval * const b) +{ + return a->tv_sec == b->tv_sec && a->tv_usec == b->tv_usec; +} + +unsigned int rin_timeval_cmp_nonequal(const struct timeval * const a, const struct timeval * const b) +{ + return a->tv_sec != b->tv_sec || a->tv_usec != b->tv_usec; +} diff --git a/src/time/time_private.h b/src/time/time_private.h index 4bb6099..097a987 100644 --- a/src/time/time_private.h +++ b/src/time/time_private.h @@ -22,5 +22,6 @@ #define LIBRIN_TIME_PRIVATE_INCLUDED static const long tv_nsec_max = 999999999l; +static const long tv_usec_max = 999999l; #endif /* LIBRIN_TIME_PRIVATE_INCLUDED */ -- cgit v1.2.3