blob: ccb30b2535360f7ff8242537f273e9361b33a6fd [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreaub7f694f2008-06-22 17:18:02 +02004 * Copyright 2000-2008 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
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
Willy Tarreaub7f694f2008-06-22 17:18:02 +020019struct timeval now; /* internal date is a monotonic function of real clock */
20struct timeval date; /* the real current date */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021struct timeval start_date; /* the process's start date */
22
23/*
24 * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
25 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020026REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020027{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020028 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
29 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020030 while (tv->tv_usec >= 1000000) {
31 tv->tv_usec -= 1000000;
32 tv->tv_sec++;
33 }
34 return tv;
35}
36
37/*
38 * 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 +020039 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020040 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020041REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020042{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020043 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020044}
45
46/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020047 * 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 +020048 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020049 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020050REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020051{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020052 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020053}
54
55/*
56 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
57 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
58 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020059 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020060 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020061REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020062{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020063 return __tv_ms_le2(tv1, tv2);
64}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020065
Willy Tarreau42aae5c2007-04-29 17:43:56 +020066/*
67 * returns the remaining time between tv1=now and event=tv2
68 * if tv2 is passed, 0 is returned.
69 * Must not be used when either argument is eternity.
70 */
71REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
72{
73 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020074}
75
76/*
77 * returns the remaining time between tv1=now and event=tv2
78 * if tv2 is passed, 0 is returned.
79 * Returns TIME_ETERNITY if tv2 is eternity.
80 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020081REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020082{
Willy Tarreaubaaee002006-06-26 02:48:02 +020083 if (tv_iseternity(tv2))
84 return TIME_ETERNITY;
85
Willy Tarreau42aae5c2007-04-29 17:43:56 +020086 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020087}
88
Willy Tarreaubaaee002006-06-26 02:48:02 +020089/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020090 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020091 * Must not be used when either argument is eternity.
92 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020093REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020094{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020095 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020096}
97
98/*
Willy Tarreaud825eef2007-05-12 22:35:00 +020099 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
100 */
101REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
102{
103 return __tv_add(tv, from, inc);
104}
105
106/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200107 * If <inc> is set, then add it to <from> and set the result to <tv>, then
108 * return 1, otherwise return 0. It is meant to be used in if conditions.
109 */
110REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
111{
112 return __tv_add_ifset(tv, from, inc);
113}
114
115/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200116 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
117 * 0 is returned. The result is stored into tv.
118 */
119REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
120{
121 return __tv_remain(tv1, tv2, tv);
122}
123
124/*
125 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
126 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
127 * eternity.
128 */
129REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
130{
131 return __tv_remain2(tv1, tv2, tv);
132}
133
Willy Tarreau0481c202007-05-13 16:03:27 +0200134/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
135REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
136{
137 return __tv_isle(tv1, tv2);
138}
139
140/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
141REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
142{
143 return __tv_isgt(tv1, tv2);
144}
145
Willy Tarreaub7f694f2008-06-22 17:18:02 +0200146/* tv_now_mono: sets <date> to the current time (wall clock), <mono> to a value
147 * following a monotonic function, and applies any required correction if the
148 * time goes backwards. Note that while we could improve it a bit by checking
149 * that the new date is not too far in the future, it is not much necessary to
150 * do so.
151 */
152REGPRM2 struct timeval *tv_now_mono(struct timeval *mono, struct timeval *wall)
153{
154 static struct timeval tv_offset;
155 struct timeval adjusted;
156
157 gettimeofday(wall, NULL);
158 __tv_add(&adjusted, wall, &tv_offset);
159 if (unlikely(__tv_islt(&adjusted, mono))) {
160 __tv_remain(wall, mono, &tv_offset);
161 return mono;
162 }
163 *mono = adjusted;
164 return mono;
165}
166
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100167char *human_time(int t, short hz_div) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200168 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
169 char *p = rv;
170 int cnt=2; // print two numbers
171
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100172 if (unlikely(t < 0 || hz_div <= 0)) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200173 sprintf(p, "?");
174 return rv;
175 }
176
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100177 if (unlikely(hz_div > 1))
178 t /= hz_div;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200179
180 if (t >= DAY) {
181 p += sprintf(p, "%dd", t / DAY);
182 cnt--;
183 }
184
185 if (cnt && t % DAY / HOUR) {
186 p += sprintf(p, "%dh", t % DAY / HOUR);
187 cnt--;
188 }
189
190 if (cnt && t % HOUR / MINUTE) {
191 p += sprintf(p, "%dm", t % HOUR / MINUTE);
192 cnt--;
193 }
194
195 if ((cnt && t % MINUTE) || !t) // also display '0s'
196 p += sprintf(p, "%ds", t % MINUTE / SEC);
197
198 return rv;
199}
200
Willy Tarreaud825eef2007-05-12 22:35:00 +0200201/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200202 * Local variables:
203 * c-indent-level: 8
204 * c-basic-offset: 8
205 * End:
206 */