From da76459dc21b5af2449af2d36eb95226cb186ce2 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 11:35:11 +0200 Subject: Adding upstream version 2.6.12. Signed-off-by: Daniel Baumann --- src/time.c | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) create mode 100644 src/time.c (limited to 'src/time.c') diff --git a/src/time.c b/src/time.c new file mode 100644 index 0000000..280b522 --- /dev/null +++ b/src/time.c @@ -0,0 +1,147 @@ +/* + * Time calculation functions. + * + * Copyright 2000-2011 Willy Tarreau + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version + * 2 of the License, or (at your option) any later version. + * + */ + +#include + +#include +#include + + +/* + * adds ms to , set the result to and returns a pointer + */ +struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms) +{ + tv->tv_usec = from->tv_usec + (ms % 1000) * 1000; + tv->tv_sec = from->tv_sec + (ms / 1000); + while (tv->tv_usec >= 1000000) { + tv->tv_usec -= 1000000; + tv->tv_sec++; + } + return tv; +} + +/* + * compares and modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 + * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that. + */ +int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_ms_cmp(tv1, tv2); +} + +/* + * compares and modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, + * assuming that TV_ETERNITY is greater than everything. + */ +int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_ms_cmp2(tv1, tv2); +} + +/* + * compares and modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2, + * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is + * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace + * occurrences of (tv_ms_cmp2(tv,now) <= 0). + */ +int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_ms_le2(tv1, tv2); +} + +/* + * returns the remaining time between tv1=now and event=tv2 + * if tv2 is passed, 0 is returned. + * Must not be used when either argument is eternity. + */ +unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_ms_remain(tv1, tv2); +} + +/* + * returns the remaining time between tv1=now and event=tv2 + * if tv2 is passed, 0 is returned. + * Returns TIME_ETERNITY if tv2 is eternity. + */ +unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2) +{ + if (tv_iseternity(tv2)) + return TIME_ETERNITY; + + return __tv_ms_remain(tv1, tv2); +} + +/* + * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2. + * Must not be used when either argument is eternity. + */ +unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_ms_elapsed(tv1, tv2); +} + +/* + * adds to , set the result to and returns a pointer + */ +struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc) +{ + return __tv_add(tv, from, inc); +} + +/* + * If is set, then add it to and set the result to , then + * return 1, otherwise return 0. It is meant to be used in if conditions. + */ +int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc) +{ + return __tv_add_ifset(tv, from, inc); +} + +/* + * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, + * 0 is returned. The result is stored into tv. + */ +struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) +{ + return __tv_remain(tv1, tv2, tv); +} + +/* + * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed, + * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is + * eternity. + */ +struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv) +{ + return __tv_remain2(tv1, tv2, tv); +} + +/* tv_isle: compares and : returns 1 if tv1 <= tv2, otherwise 0 */ +int _tv_isle(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_isle(tv1, tv2); +} + +/* tv_isgt: compares and : returns 1 if tv1 > tv2, otherwise 0 */ +int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2) +{ + return __tv_isgt(tv1, tv2); +} + +/* + * Local variables: + * c-indent-level: 8 + * c-basic-offset: 8 + * End: + */ -- cgit v1.2.3