2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* Time calculation functions.
|
|
|
|
*
|
2007-04-29 15:43:56 +00:00
|
|
|
* Copyright 2000-2007 Willy Tarreau <w@1wt.eu>
|
2006-06-26 00:48:02 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/time.h>
|
2006-06-29 16:54:54 +00:00
|
|
|
|
|
|
|
#include <common/config.h>
|
2007-02-11 23:59:08 +00:00
|
|
|
#include <common/standard.h>
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/time.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
struct timeval now; /* the current date at any moment */
|
|
|
|
struct timeval start_date; /* the process's start date */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv>
|
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM3 struct timeval *_tv_ms_add(struct timeval *tv, const struct timeval *from, int ms)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2007-04-29 15:43:56 +00:00
|
|
|
tv->tv_usec = from->tv_usec + (ms % 1000) * 1000;
|
|
|
|
tv->tv_sec = from->tv_sec + (ms / 1000);
|
2006-06-26 00:48:02 +00:00
|
|
|
while (tv->tv_usec >= 1000000) {
|
|
|
|
tv->tv_usec -= 1000000;
|
|
|
|
tv->tv_sec++;
|
|
|
|
}
|
|
|
|
return tv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2
|
2007-04-29 15:43:56 +00:00
|
|
|
* Must not be used when either argument is eternity. Use tv_ms_cmp2() for that.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM2 int _tv_ms_cmp(const struct timeval *tv1, const struct timeval *tv2)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2007-04-29 15:43:56 +00:00
|
|
|
return __tv_ms_cmp(tv1, tv2);
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2,
|
2007-04-28 20:40:08 +00:00
|
|
|
* assuming that TV_ETERNITY is greater than everything.
|
2006-06-26 00:48:02 +00:00
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM2 int _tv_ms_cmp2(const struct timeval *tv1, const struct timeval *tv2)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2007-04-29 15:43:56 +00:00
|
|
|
return __tv_ms_cmp2(tv1, tv2);
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
2007-04-29 08:50:43 +00:00
|
|
|
/*
|
|
|
|
* compares <tv1> and <tv2> modulo 1 ms: returns 1 if tv1 <= tv2, 0 if tv1 > tv2,
|
|
|
|
* assuming that TV_ETERNITY is greater than everything. Returns 0 if tv1 is
|
|
|
|
* TV_ETERNITY, and always assumes that tv2 != TV_ETERNITY. Designed to replace
|
2007-04-29 15:43:56 +00:00
|
|
|
* occurrences of (tv_ms_cmp2(tv,now) <= 0).
|
2007-04-29 08:50:43 +00:00
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM2 int _tv_ms_le2(const struct timeval *tv1, const struct timeval *tv2)
|
2007-04-29 08:50:43 +00:00
|
|
|
{
|
2007-04-29 15:43:56 +00:00
|
|
|
return __tv_ms_le2(tv1, tv2);
|
|
|
|
}
|
2007-04-29 08:50:43 +00:00
|
|
|
|
2007-04-29 15:43:56 +00:00
|
|
|
/*
|
|
|
|
* returns the remaining time between tv1=now and event=tv2
|
|
|
|
* if tv2 is passed, 0 is returned.
|
|
|
|
* Must not be used when either argument is eternity.
|
|
|
|
*/
|
|
|
|
REGPRM2 unsigned long _tv_ms_remain(const struct timeval *tv1, const struct timeval *tv2)
|
|
|
|
{
|
|
|
|
return __tv_ms_remain(tv1, tv2);
|
2007-04-29 08:50:43 +00:00
|
|
|
}
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
/*
|
|
|
|
* returns the remaining time between tv1=now and event=tv2
|
|
|
|
* if tv2 is passed, 0 is returned.
|
|
|
|
* Returns TIME_ETERNITY if tv2 is eternity.
|
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM2 unsigned long _tv_ms_remain2(const struct timeval *tv1, const struct timeval *tv2)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
|
|
|
if (tv_iseternity(tv2))
|
|
|
|
return TIME_ETERNITY;
|
|
|
|
|
2007-04-29 15:43:56 +00:00
|
|
|
return __tv_ms_remain(tv1, tv2);
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-04-29 15:43:56 +00:00
|
|
|
* Returns the time in ms elapsed between tv1 and tv2, assuming that tv1<=tv2.
|
2006-06-26 00:48:02 +00:00
|
|
|
* Must not be used when either argument is eternity.
|
|
|
|
*/
|
2007-04-29 15:43:56 +00:00
|
|
|
REGPRM2 unsigned long _tv_ms_elapsed(const struct timeval *tv1, const struct timeval *tv2)
|
2006-06-26 00:48:02 +00:00
|
|
|
{
|
2007-04-29 15:43:56 +00:00
|
|
|
return __tv_ms_elapsed(tv1, tv2);
|
2006-06-26 00:48:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|