blob: 661c7e25ad5ffc8dd9c24aabfd1ec357d19811fa [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * Time calculation functions.
3 *
Willy Tarreau42aae5c2007-04-29 17:43:56 +02004 * Copyright 2000-2007 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
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 Tarreau42aae5c2007-04-29 17:43:56 +020025REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
Willy Tarreaubaaee002006-06-26 02:48:02 +020026{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020027 tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
28 tv->tv_sec = from->tv_sec + (ms / 1000);
Willy Tarreaubaaee002006-06-26 02:48:02 +020029 while (tv->tv_usec >= 1000000) {
30 tv->tv_usec -= 1000000;
31 tv->tv_sec++;
32 }
33 return tv;
34}
35
36/*
37 * 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 +020038 * Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
Willy Tarreaubaaee002006-06-26 02:48:02 +020039 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020040REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020041{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020042 return __tv_ms_cmp(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020043}
44
45/*
Willy Tarreaubaaee002006-06-26 02:48:02 +020046 * 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 +020047 * assuming that TV_ETERNITY is greater than everything.
Willy Tarreaubaaee002006-06-26 02:48:02 +020048 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020049REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020050{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020051 return __tv_ms_cmp2(tv1, tv2);
Willy Tarreau8d7d1492007-04-29 10:50:43 +020052}
53
54/*
55 * compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
56 * assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
57 * TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
Willy Tarreau42aae5c2007-04-29 17:43:56 +020058 * occurrences of (tv_ms_cmp2(tv,now) <= 0).
Willy Tarreau8d7d1492007-04-29 10:50:43 +020059 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020060REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreau8d7d1492007-04-29 10:50:43 +020061{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020062 return __tv_ms_le2(tv1, tv2);
63}
Willy Tarreau8d7d1492007-04-29 10:50:43 +020064
Willy Tarreau42aae5c2007-04-29 17:43:56 +020065/*
66 * returns the remaining time between tv1=now and event=tv2
67 * if tv2 is passed, 0 is returned.
68 * Must not be used when either argument is eternity.
69 */
70REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
71{
72 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020073}
74
75/*
76 * returns the remaining time between tv1=now and event=tv2
77 * if tv2 is passed, 0 is returned.
78 * Returns TIME_ETERNITY if tv2 is eternity.
79 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020080REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020081{
Willy Tarreaubaaee002006-06-26 02:48:02 +020082 if (tv_iseternity(tv2))
83 return TIME_ETERNITY;
84
Willy Tarreau42aae5c2007-04-29 17:43:56 +020085 return __tv_ms_remain(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020086}
87
Willy Tarreaubaaee002006-06-26 02:48:02 +020088/*
Willy Tarreau42aae5c2007-04-29 17:43:56 +020089 * Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
Willy Tarreaubaaee002006-06-26 02:48:02 +020090 * Must not be used when either argument is eternity.
91 */
Willy Tarreau42aae5c2007-04-29 17:43:56 +020092REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
Willy Tarreaubaaee002006-06-26 02:48:02 +020093{
Willy Tarreau42aae5c2007-04-29 17:43:56 +020094 return __tv_ms_elapsed(tv1, tv2);
Willy Tarreaubaaee002006-06-26 02:48:02 +020095}
96
97/*
Willy Tarreaud825eef2007-05-12 22:35:00 +020098 * adds <inc> to <from>, set the result to <tv> and returns a pointer <tv>
99 */
100REGPRM3 struct timeval *_tv_add(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
101{
102 return __tv_add(tv, from, inc);
103}
104
105/*
Willy Tarreau0481c202007-05-13 16:03:27 +0200106 * If <inc> is set, then add it to <from> and set the result to <tv>, then
107 * return 1, otherwise return 0. It is meant to be used in if conditions.
108 */
109REGPRM3 int _tv_add_ifset(struct timeval *tv, const struct timeval *from, const struct timeval *inc)
110{
111 return __tv_add_ifset(tv, from, inc);
112}
113
114/*
Willy Tarreaud825eef2007-05-12 22:35:00 +0200115 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
116 * 0 is returned. The result is stored into tv.
117 */
118REGPRM3 struct timeval *_tv_remain(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
119{
120 return __tv_remain(tv1, tv2, tv);
121}
122
123/*
124 * Computes the remaining time between tv1=now and event=tv2. if tv2 is passed,
125 * 0 is returned. The result is stored into tv. Returns ETERNITY if tv2 is
126 * eternity.
127 */
128REGPRM3 struct timeval *_tv_remain2(const struct timeval *tv1, const struct timeval *tv2, struct timeval *tv)
129{
130 return __tv_remain2(tv1, tv2, tv);
131}
132
Willy Tarreau0481c202007-05-13 16:03:27 +0200133/* tv_isle: compares <tv1> and <tv2> : returns 1 if tv1 <= tv2, otherwise 0 */
134REGPRM2 int _tv_isle(const struct timeval *tv1, const struct timeval *tv2)
135{
136 return __tv_isle(tv1, tv2);
137}
138
139/* tv_isgt: compares <tv1> and <tv2> : returns 1 if tv1 > tv2, otherwise 0 */
140REGPRM2 int _tv_isgt(const struct timeval *tv1, const struct timeval *tv2)
141{
142 return __tv_isgt(tv1, tv2);
143}
144
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100145char *human_time(int t, short hz_div) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200146 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
147 char *p = rv;
148 int cnt=2; // print two numbers
149
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100150 if (unlikely(t < 0 || hz_div <= 0)) {
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200151 sprintf(p, "?");
152 return rv;
153 }
154
Willy Tarreauc8f24f82007-11-30 18:38:35 +0100155 if (unlikely(hz_div > 1))
156 t /= hz_div;
Krzysztof Oledzki85130942007-10-22 16:21:10 +0200157
158 if (t >= DAY) {
159 p += sprintf(p, "%dd", t / DAY);
160 cnt--;
161 }
162
163 if (cnt && t % DAY / HOUR) {
164 p += sprintf(p, "%dh", t % DAY / HOUR);
165 cnt--;
166 }
167
168 if (cnt && t % HOUR / MINUTE) {
169 p += sprintf(p, "%dm", t % HOUR / MINUTE);
170 cnt--;
171 }
172
173 if ((cnt && t % MINUTE) || !t) // also display '0s'
174 p += sprintf(p, "%ds", t % MINUTE / SEC);
175
176 return rv;
177}
178
Willy Tarreaud825eef2007-05-12 22:35:00 +0200179/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200180 * Local variables:
181 * c-indent-level: 8
182 * c-basic-offset: 8
183 * End:
184 */