blob: 280b522b8287bc88ae99840beb95140e03e23869 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreau45a12512011-09-10 16:56:42 +02004 * Copyright 2000-2011 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020014
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020015#include <haproxy/api.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020016#include <haproxy/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017
Willy Tarreau93acfa22019-09-26 08:00:23 +020018
Willy Tarreaubaaee002006-06-26 02:48:02 +020019/*
20 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
21 */
Willy Tarreau03e78532020-02-25 07:38:05 +010022struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020023{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020024 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
25 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020026 while (tv->tv_usec >= 1000000) {
27 tv->tv_usec -= 1000000;
28 tv->tv_sec++;
29 }
30 return tv;
31}
32
33/*
34 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
Willy Tarreau42aae5c2007-04-29 17:43:56 +020035 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020036 */
Willy Tarreau03e78532020-02-25 07:38:05 +010037int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020038{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020039 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020040}
41
42/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020043 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020044 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020045 */
Willy Tarreau03e78532020-02-25 07:38:05 +010046int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020047{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020048 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020049}
50
51/*
52 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
53 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
54 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020055 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020056 */
Willy Tarreau03e78532020-02-25 07:38:05 +010057int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020058{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020059 return __tv_ms_le2(tv1, tv2);
60}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020061
Willy Tarreau42aae5c2007-04-29 17:43:56 +020062/*
63 * returns the remaining time between tv1=now and event=tv2
64 * if tv2 is passed, 0 is returned.
65 * Must not be used when either argument is eternity.
66 */
Willy Tarreau03e78532020-02-25 07:38:05 +010067unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau42aae5c2007-04-29 17:43:56 +020068{
69 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070}
71
72/*
73 * returns the remaining time between tv1=now and event=tv2
74 * if tv2 is passed, 0 is returned.
75 * Returns TIME_ETERNITY if tv2 is eternity.
76 */
Willy Tarreau03e78532020-02-25 07:38:05 +010077unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020078{
Willy Tarreaubaaee002006-06-26 02:48:02 +020079 if (tv_iseternity(tv2))
80 return TIME_ETERNITY;
81
Willy Tarreau42aae5c2007-04-29 17:43:56 +020082 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020083}
84
Willy Tarreaubaaee002006-06-26 02:48:02 +020085/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020087 * Must not be used when either argument is eternity.
88 */
Willy Tarreau03e78532020-02-25 07:38:05 +010089unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020090{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020091 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020092}
93
94/*
Willy Tarreaud825eef2007-05-12 22:35:00 +020095 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
96 */
Willy Tarreau03e78532020-02-25 07:38:05 +010097struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreaud825eef2007-05-12 22:35:00 +020098{
99 return __tv_add(tv, from, inc);
100}
101
102/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200103 * If <inc> is set, then add it to <from> and set the result to <tv>, then
104 * return 1, otherwise return 0. It is meant to be used in if conditions.
105 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100106int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
Willy Tarreau0481c202007-05-13 16:03:27 +0200107{
108 return __tv_add_ifset(tv, from, inc);
109}
110
111/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200112 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
113 * 0 is returned. The result is stored into tv.
114 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100115struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116{
117 return __tv_remain(tv1, tv2, tv);
118}
119
120/*
121 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
122 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
123 * eternity.
124 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100125struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
Willy Tarreaud825eef2007-05-12 22:35:00 +0200126{
127 return __tv_remain2(tv1, tv2, tv);
128}
129
Willy Tarreau0481c202007-05-13 16:03:27 +0200130/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100131int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200132{
133 return __tv_isle(tv1, tv2);
134}
135
136/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
Willy Tarreau03e78532020-02-25 07:38:05 +0100137int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau0481c202007-05-13 16:03:27 +0200138{
139 return __tv_isgt(tv1, tv2);
140}
141
Willy Tarreaud825eef2007-05-12 22:35:00 +0200142/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 * Local variables:
144 * c-indent-level: 8
145 * c-basic-offset: 8
146 * End:
147 */