#include <sys/types.h>
#include <sys/time.h>
#include <assert.h>
#include <stdio.h>
#include "tod.h"
#define USP 1000000
int tod_cmp (const struct timeval *a, const struct timeval *b)
{
int rc;
assert (a->tv_usec <= USP);
assert (b->tv_usec <= USP);
rc = a->tv_sec - b->tv_sec;
if (rc == 0)
rc = a->tv_usec - b->tv_usec;
return rc;
}
int tod_lt (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) < 0;
}
int tod_gt (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) > 0;
}
int tod_lte (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) <= 0;
}
int tod_gte (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) >= 0;
}
int tod_eq (const struct timeval *a, const struct timeval *b)
{
return tod_cmp (a, b) == 0;
}
void tod_addto (struct timeval *a, const struct timeval *b)
{
a->tv_usec += b->tv_usec;
a->tv_sec += b->tv_sec + a->tv_usec / USP;
a->tv_usec %= USP;
}
void tod_subfrom (struct timeval *a, struct timeval b)
{
assert (a->tv_usec <= USP);
assert (b.tv_usec <= USP);
if (b.tv_usec > a->tv_usec)
{
a->tv_usec += USP;
a->tv_sec -= 1;
}
a->tv_usec -= b.tv_usec;
a->tv_sec -= b.tv_sec;
}
void tod_gettime (struct timeval *tp)
{
gettimeofday (tp, NULL);
tp->tv_sec += tp->tv_usec / USP;
tp->tv_usec %= USP;
}