blob: b25846662240f1749a8da88686be980c468d6ee2 [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>
27
28#define TIME_ETERNITY -1
29
30/* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */
31#define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old)))
32#define SETNOW(a) (*a=now)
33
34extern struct timeval now; /* the current date at any moment */
35extern struct timeval start_date; /* the process's start date */
36
37
38/*
39 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
40 */
41struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms);
42
43/*
44 * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
45 * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that.
46 */
47int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2);
48
49/*
50 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
51 * considering that 0 is the eternity.
52 */
53int tv_cmp2(struct timeval *tv1, struct timeval *tv2);
54/*
55 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
56 * considering that 0 is the eternity.
57 */
58int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2);
59
60/*
61 * returns the remaining time between tv1=now and event=tv2
62 * if tv2 is passed, 0 is returned.
63 * Returns TIME_ETERNITY if tv2 is eternity.
64 */
65unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2);
66
67
68/* sets <tv> to the current time */
69static inline struct timeval *tv_now(struct timeval *tv)
70{
71 if (tv)
72 gettimeofday(tv, NULL);
73 return tv;
74}
75
76/*
77 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
78 * Must not be used when either argument is eternity. Use tv_cmp2() for that.
79 */
80static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2)
81{
82 if (tv1->tv_sec < tv2->tv_sec)
83 return -1;
84 else if (tv1->tv_sec > tv2->tv_sec)
85 return 1;
86 else if (tv1->tv_usec < tv2->tv_usec)
87 return -1;
88 else if (tv1->tv_usec > tv2->tv_usec)
89 return 1;
90 else
91 return 0;
92}
93
94/*
95 * returns the difference, in ms, between tv1 and tv2
96 * Must not be used when either argument is eternity.
97 */
98static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2)
99{
100 unsigned long ret;
101
102 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
103 if (tv2->tv_usec > tv1->tv_usec)
104 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
105 else
106 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
107 return (unsigned long) ret;
108}
109
110/*
111 * returns the remaining time between tv1=now and event=tv2
112 * if tv2 is passed, 0 is returned.
113 * Must not be used when either argument is eternity.
114 */
115static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2)
116{
117 unsigned long ret;
118
119 if (tv_cmp_ms(tv1, tv2) >= 0)
120 return 0; /* event elapsed */
121
122 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
123 if (tv2->tv_usec > tv1->tv_usec)
124 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
125 else
126 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
127 return (unsigned long) ret;
128}
129
130
131/*
132 * zeroes a struct timeval
133 */
134
135static inline struct timeval *tv_eternity(struct timeval *tv)
136{
137 tv->tv_sec = tv->tv_usec = 0;
138 return tv;
139}
140
141/*
142 * returns 1 if tv is null, else 0
143 */
144static inline int tv_iseternity(struct timeval *tv)
145{
146 if (tv->tv_sec == 0 && tv->tv_usec == 0)
147 return 1;
148 else
149 return 0;
150}
151
152/*
153 * returns the first event between tv1 and tv2 into tvmin.
154 * a zero tv is ignored. tvmin is returned.
155 */
156static inline struct timeval *tv_min(struct timeval *tvmin,
157 struct timeval *tv1, struct timeval *tv2)
158{
159
160 if (tv_cmp2(tv1, tv2) <= 0)
161 *tvmin = *tv1;
162 else
163 *tvmin = *tv2;
164
165 return tvmin;
166}
167
168
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200169#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200170
171/*
172 * Local variables:
173 * c-indent-level: 8
174 * c-basic-offset: 8
175 * End:
176 */