Add handy functions for timeval manipulations. Obtained from FreeBSD.
This commit is contained in:
committed by
Justin Maggard
parent
63f725f8c9
commit
04e243c85c
40
utils.c
40
utils.c
@ -531,3 +531,43 @@ valid_media_types(const char *path)
|
||||
|
||||
return ALL_MEDIA;
|
||||
}
|
||||
|
||||
/*
|
||||
* Add and subtract routines for timevals.
|
||||
* N.B.: subtract routine doesn't deal with
|
||||
* results which are before the beginning,
|
||||
* it just gets very confused in this case.
|
||||
* Caveat emptor.
|
||||
*/
|
||||
static void timevalfix(struct timeval *);
|
||||
void
|
||||
timevaladd(struct timeval *t1, const struct timeval *t2)
|
||||
{
|
||||
|
||||
t1->tv_sec += t2->tv_sec;
|
||||
t1->tv_usec += t2->tv_usec;
|
||||
timevalfix(t1);
|
||||
}
|
||||
|
||||
void
|
||||
timevalsub(struct timeval *t1, const struct timeval *t2)
|
||||
{
|
||||
|
||||
t1->tv_sec -= t2->tv_sec;
|
||||
t1->tv_usec -= t2->tv_usec;
|
||||
timevalfix(t1);
|
||||
}
|
||||
|
||||
static void
|
||||
timevalfix(struct timeval *t1)
|
||||
{
|
||||
|
||||
if (t1->tv_usec < 0) {
|
||||
t1->tv_sec--;
|
||||
t1->tv_usec += 1000000;
|
||||
}
|
||||
if (t1->tv_usec >= 1000000) {
|
||||
t1->tv_sec++;
|
||||
t1->tv_usec -= 1000000;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user