blob: 262bc44911ea94bb51fe1d7dea0d004735c7f222 [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/*
98 * Local variables:
99 * c-indent-level: 8
100 * c-basic-offset: 8
101 * End:
102 */