blob: 36c5ca5a2b16d04681cdda9e7eb8f5078f531b8d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau2dd0d472006-06-29 17:53:05 +02002 include/common/time.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 Time calculation functions and macros.
4
5 Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation, version 2.1
10 exclusively.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
Willy Tarreau2dd0d472006-06-29 17:53:05 +020022#ifndef _COMMON_TIME_H
23#define _COMMON_TIME_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
25#include <stdlib.h>
26#include <sys/time.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020027#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29#define TIME_ETERNITY -1
30
31/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
32#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
33#define SETNOW(a) (*a=now)
34
35extern struct timeval now; /* the current date at any moment */
36extern struct timeval start_date; /* the process's start date */
37
38
39/*
40 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
41 */
Willy Tarreaufb278672006-10-15 15:38:50 +020042REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms);
Willy Tarreaubaaee002006-06-26 02:48:02 +020043
44/*
45 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
46 * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
47 */
Willy Tarreaufb278672006-10-15 15:38:50 +020048REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020049
50/*
Willy Tarreau5e8f0662007-02-12 00:59:08 +010051 * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2,
52 * considering that 0 is the eternity.
53 */
54REGPRM2 int tv_cmp_ge2(const struct timeval *tv1, const struct timeval *tv2);
55
56/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
58 * considering that 0 is the eternity.
59 */
Willy Tarreaufb278672006-10-15 15:38:50 +020060REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreau5e8f0662007-02-12 00:59:08 +010061
Willy Tarreaubaaee002006-06-26 02:48:02 +020062/*
63 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
64 * considering that 0 is the eternity.
65 */
Willy Tarreaufb278672006-10-15 15:38:50 +020066REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
68/*
69 * returns the remaining time between tv1=now and event=tv2
70 * if tv2 is passed, 0 is returned.
71 * Returns TIME_ETERNITY if tv2 is eternity.
72 */
Willy Tarreaufb278672006-10-15 15:38:50 +020073REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074
75
76/* sets <tv> to the current time */
Willy Tarreaufb278672006-10-15 15:38:50 +020077REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +020078{
79 if (tv)
80 gettimeofday(tv, NULL);
81 return tv;
82}
83
84/*
85 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
86 * Must not be used when either argument is eternity. Use tv_cmp2() for that.
87 */
Willy Tarreaufb278672006-10-15 15:38:50 +020088REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020089{
90 if (tv1->tv_sec < tv2->tv_sec)
91 return -1;
92 else if (tv1->tv_sec > tv2->tv_sec)
93 return 1;
94 else if (tv1->tv_usec < tv2->tv_usec)
95 return -1;
96 else if (tv1->tv_usec > tv2->tv_usec)
97 return 1;
98 else
99 return 0;
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100100}
101
102/*
103 * compares <tv1> and <tv2> : returns 0 if tv1 < tv2, 1 if tv1 >= tv2
104 */
105REGPRM2 static inline int tv_cmp_ge(const struct timeval *tv1, const struct timeval *tv2)
106{
107 if (tv1->tv_sec > tv2->tv_sec)
108 return 1;
109 if (tv1->tv_sec < tv2->tv_sec)
110 return 0;
111 if (tv1->tv_usec >= tv2->tv_usec)
112 return 1;
113 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200114}
115
116/*
117 * returns the difference, in ms, between tv1 and tv2
118 * Must not be used when either argument is eternity.
119 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200120REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200121{
122 unsigned long ret;
123
124 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
125 if (tv2->tv_usec > tv1->tv_usec)
126 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
127 else
128 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
129 return (unsigned long) ret;
130}
131
132/*
133 * returns the remaining time between tv1=now and event=tv2
134 * if tv2 is passed, 0 is returned.
135 * Must not be used when either argument is eternity.
136 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200137REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138{
139 unsigned long ret;
140
141 if (tv_cmp_ms(tv1, tv2) >= 0)
142 return 0; /* event elapsed */
143
144 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
145 if (tv2->tv_usec > tv1->tv_usec)
146 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
147 else
148 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
149 return (unsigned long) ret;
150}
151
152
153/*
154 * zeroes a struct timeval
155 */
156
Willy Tarreaufb278672006-10-15 15:38:50 +0200157REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158{
159 tv->tv_sec = tv->tv_usec = 0;
160 return tv;
161}
162
163/*
164 * returns 1 if tv is null, else 0
165 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200166REGPRM1 static inline int tv_iseternity(const struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167{
Willy Tarreau5e8f0662007-02-12 00:59:08 +0100168 if ((tv->tv_sec | tv->tv_usec) == 0)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200169 return 1;
170 else
171 return 0;
172}
173
174/*
175 * returns the first event between tv1 and tv2 into tvmin.
176 * a zero tv is ignored. tvmin is returned.
177 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200178REGPRM3 static inline const struct timeval *tv_min(struct timeval *tvmin,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200179 const struct timeval *tv1,
180 const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181{
182
183 if (tv_cmp2(tv1, tv2) <= 0)
184 *tvmin = *tv1;
185 else
186 *tvmin = *tv2;
187
188 return tvmin;
189}
190
191
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200192#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193
194/*
195 * Local variables:
196 * c-indent-level: 8
197 * c-basic-offset: 8
198 * End:
199 */