blob: 1546921e98459093914a8bd9ac618801cf5d5c1d [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/*
51 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
52 * considering that 0 is the eternity.
53 */
Willy Tarreaufb278672006-10-15 15:38:50 +020054REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020055/*
56 * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
57 * considering that 0 is the eternity.
58 */
Willy Tarreaufb278672006-10-15 15:38:50 +020059REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020060
61/*
62 * returns the remaining time between tv1=now and event=tv2
63 * if tv2 is passed, 0 is returned.
64 * Returns TIME_ETERNITY if tv2 is eternity.
65 */
Willy Tarreaufb278672006-10-15 15:38:50 +020066REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020067
68
69/* sets <tv> to the current time */
Willy Tarreaufb278672006-10-15 15:38:50 +020070REGPRM1 static inline struct timeval *tv_now(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +020071{
72 if (tv)
73 gettimeofday(tv, NULL);
74 return tv;
75}
76
77/*
78 * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
79 * Must not be used when either argument is eternity. Use tv_cmp2() for that.
80 */
Willy Tarreaufb278672006-10-15 15:38:50 +020081REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082{
83 if (tv1->tv_sec < tv2->tv_sec)
84 return -1;
85 else if (tv1->tv_sec > tv2->tv_sec)
86 return 1;
87 else if (tv1->tv_usec < tv2->tv_usec)
88 return -1;
89 else if (tv1->tv_usec > tv2->tv_usec)
90 return 1;
91 else
92 return 0;
93}
94
95/*
96 * returns the difference, in ms, between tv1 and tv2
97 * Must not be used when either argument is eternity.
98 */
Willy Tarreaufb278672006-10-15 15:38:50 +020099REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200100{
101 unsigned long ret;
102
103 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
104 if (tv2->tv_usec > tv1->tv_usec)
105 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
106 else
107 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
108 return (unsigned long) ret;
109}
110
111/*
112 * returns the remaining time between tv1=now and event=tv2
113 * if tv2 is passed, 0 is returned.
114 * Must not be used when either argument is eternity.
115 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200116REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117{
118 unsigned long ret;
119
120 if (tv_cmp_ms(tv1, tv2) >= 0)
121 return 0; /* event elapsed */
122
123 ret = (tv2->tv_sec - tv1->tv_sec) * 1000;
124 if (tv2->tv_usec > tv1->tv_usec)
125 ret += (tv2->tv_usec - tv1->tv_usec) / 1000;
126 else
127 ret -= (tv1->tv_usec - tv2->tv_usec) / 1000;
128 return (unsigned long) ret;
129}
130
131
132/*
133 * zeroes a struct timeval
134 */
135
Willy Tarreaufb278672006-10-15 15:38:50 +0200136REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200137{
138 tv->tv_sec = tv->tv_usec = 0;
139 return tv;
140}
141
142/*
143 * returns 1 if tv is null, else 0
144 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200145REGPRM1 static inline int tv_iseternity(const struct timeval *tv)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146{
147 if (tv->tv_sec == 0 && tv->tv_usec == 0)
148 return 1;
149 else
150 return 0;
151}
152
153/*
154 * returns the first event between tv1 and tv2 into tvmin.
155 * a zero tv is ignored. tvmin is returned.
156 */
Willy Tarreaufb278672006-10-15 15:38:50 +0200157REGPRM3 static inline const struct timeval *tv_min(struct timeval *tvmin,
Willy Tarreaub17916e2006-10-15 15:17:57 +0200158 const struct timeval *tv1,
159 const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200160{
161
162 if (tv_cmp2(tv1, tv2) <= 0)
163 *tvmin = *tv1;
164 else
165 *tvmin = *tv2;
166
167 return tvmin;
168}
169
170
Willy Tarreau2dd0d472006-06-29 17:53:05 +0200171#endif /* _COMMON_TIME_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172
173/*
174 * Local variables:
175 * c-indent-level: 8
176 * c-basic-offset: 8
177 * End:
178 */