blob: d94e364730f7b731f5d9f7abd37e0f9410627c87 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
4 * Copyright 2000-2006 Willy Tarreau <w@1wt.eu>
5 *
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
15#include <common/config.h>
Willy Tarreau5e8f0662007-02-12 00:59:08 +010016#include <common/standard.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020017#include <common/time.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020018
19struct timeval now; /* the current date at any moment */
20struct timeval start_date; /* the process's start date */
21
22/*
23 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
24 */
Willy Tarreaufb278672006-10-15 15:38:50 +020025REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020026{
27 if (!tv || !from)
28 return NULL;
29 tv->tv_usec = from->tv_usec + (ms%1000)*1000;
30 tv->tv_sec = from->tv_sec + (ms/1000);
31 while (tv->tv_usec >= 1000000) {
32 tv->tv_usec -= 1000000;
33 tv->tv_sec++;
34 }
35 return tv;
36}
37
38/*
39 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
40 * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
41 */
Willy Tarreaufb278672006-10-15 15:38:50 +020042REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020043{
Willy Tarreaua6a6a932007-04-28 22:40:08 +020044 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
45 if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020047 else if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
Willy Tarreaubaaee002006-06-26 02:48:02 +020048 return 1;
49 else
50 return 0;
51 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +020052 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
53 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
54 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
Willy Tarreaubaaee002006-06-26 02:48:02 +020055 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020056 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
57 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
58 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreaubaaee002006-06-26 02:48:02 +020059 return 1;
60 else
61 return 0;
62}
63
64/*
Willy Tarreau5e8f0662007-02-12 00:59:08 +010065 * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020066 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreau5e8f0662007-02-12 00:59:08 +010067 */
68REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2)
69{
Willy Tarreaua6a6a932007-04-28 22:40:08 +020070 if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +010071 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020072 if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +010073 return 0;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020074 if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec)
Willy Tarreau5e8f0662007-02-12 00:59:08 +010075 return 1;
76 return 0;
77}
78
79/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020080 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
Willy Tarreaua6a6a932007-04-28 22:40:08 +020081 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 */
Willy Tarreaufb278672006-10-15 15:38:50 +020083REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020084{
Willy Tarreaua6a6a932007-04-28 22:40:08 +020085 if ((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +020086 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020087 else if ((unsigned)tv1->tv_sec < (unsigned)tv2->tv_sec)
Willy Tarreaubaaee002006-06-26 02:48:02 +020088 return -1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020089 else if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +020091 else if ((unsigned)tv1->tv_usec < (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +020092 return -1;
93 else
94 return 0;
95}
96
97/*
98 * 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 +020099 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200101REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200102{
103 if (tv_iseternity(tv1))
104 if (tv_iseternity(tv2))
105 return 0; /* same */
106 else
107 return 1; /* tv1 later than tv2 */
108 else if (tv_iseternity(tv2))
109 return -1; /* tv2 later than tv1 */
110
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200111 if ((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec) {
112 if ((unsigned)tv1->tv_usec >= (unsigned)tv2->tv_usec + 1000)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200113 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200114 else if ((unsigned)tv2->tv_usec >= (unsigned)tv1->tv_usec + 1000)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200115 return -1;
116 else
117 return 0;
118 }
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200119 else if (((unsigned)tv1->tv_sec > (unsigned)tv2->tv_sec + 1) ||
120 (((unsigned)tv1->tv_sec == (unsigned)tv2->tv_sec + 1) &&
121 ((unsigned)tv1->tv_usec + 1000000 >= (unsigned)tv2->tv_usec + 1000)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200122 return 1;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200123 else if (((unsigned)tv2->tv_sec > (unsigned)tv1->tv_sec + 1) ||
124 (((unsigned)tv2->tv_sec == (unsigned)tv1->tv_sec + 1) &&
125 ((unsigned)tv2->tv_usec + 1000000 >= (unsigned)tv1->tv_usec + 1000)))
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 return -1;
127 else
128 return 0;
129}
130
131/*
132 * returns the remaining time between tv1=now and event=tv2
133 * if tv2 is passed, 0 is returned.
134 * Returns TIME_ETERNITY if tv2 is eternity.
135 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200136REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137{
138 unsigned long ret;
139
140 if (tv_iseternity(tv2))
141 return TIME_ETERNITY;
142
143 if (tv_cmp_ms(tv1, tv2) >= 0)
144 return 0; /* event elapsed */
145
146 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200147 if ((unsigned)tv2->tv_usec > (unsigned)tv1->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
149 else
150 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
151 return (unsigned long) ret;
152}
153
154
155/*
156 * returns the absolute difference, in ms, between tv1 and tv2
157 * Must not be used when either argument is eternity.
158 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200159REGPRM2 unsigned long tv_delta(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160{
161 int cmp;
162 unsigned long ret;
163
164
165 cmp = tv_cmp(tv1, tv2);
166 if (!cmp)
167 return 0; /* same dates, null diff */
168 else if (cmp < 0) {
Willy Tarreaub17916e2006-10-15 15:17:57 +0200169 const struct timeval *tmp = tv1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170 tv1 = tv2;
171 tv2 = tmp;
172 }
173 ret = (tv1->tv_sec - tv2->tv_sec) * 1000;
Willy Tarreaua6a6a932007-04-28 22:40:08 +0200174 if ((unsigned)tv1->tv_usec > (unsigned)tv2->tv_usec)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 ret += (tv1->tv_usec - tv2->tv_usec) / 1000;
176 else
177 ret -= (tv2->tv_usec - tv1->tv_usec) / 1000;
178 return (unsigned long) ret;
179}
180
181/*
182 * Local variables:
183 * c-indent-level: 8
184 * c-basic-offset: 8
185 * End:
186 */