Create header tile for timeutils, rename tval.c to time.c

This commit is contained in:
Nils O. Selåsdal
2013-12-25 19:42:40 +01:00
parent a147b868c0
commit 9756ca0aeb
2 changed files with 132 additions and 2 deletions
+99
View File
@@ -0,0 +1,99 @@
#include "ucore/time.h"
struct timeval *
uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b)
{
dst->tv_sec = a->tv_sec + b->tv_sec;
dst->tv_usec = a->tv_usec + b->tv_usec;
if (dst->tv_usec >= 1000000) {
dst->tv_sec++;
dst->tv_usec -= 1000000;
}
return dst;
}
struct timeval *
uc_tvsub(struct timeval *dst, const struct timeval *a, const struct timeval *b)
{
dst->tv_sec = a->tv_sec - b->tv_sec;
dst->tv_usec = a->tv_usec - b->tv_usec;
if (dst->tv_usec < 0) {
dst->tv_sec--;
dst->tv_usec += 1000000;
}
return dst;
}
int
uc_tvcmp(const struct timeval *a, const struct timeval *b)
{
if (a->tv_sec < b->tv_sec)
return -1;
else if (a->tv_sec > b->tv_sec)
return 1;
if (a->tv_usec < b->tv_usec)
return -1;
else if (a->tv_usec > b->tv_usec)
return 1;
return 0;
}
struct timespec *
uc_tsadd(struct timespec *dst, const struct timespec *a, const struct timespec *b)
{
dst->tv_sec = a->tv_sec + b->tv_sec;
dst->tv_nsec = a->tv_nsec + b->tv_nsec;
if (dst->tv_nsec >= 1000000000) {
dst->tv_sec++;
dst->tv_nsec -= 1000000000;
}
return dst;
}
struct timespec *
uc_tssub(struct timespec *dst, const struct timespec *a, const struct timespec *b)
{
dst->tv_sec = a->tv_sec - b->tv_sec;
dst->tv_nsec = a->tv_nsec - b->tv_nsec;
if (dst->tv_nsec < 0) {
dst->tv_sec--;
dst->tv_nsec += 1000000000;
}
return dst;
}
int
uc_tscmp(const struct timespec *a, const struct timespec *b)
{
if (a->tv_sec < b->tv_sec)
return -1;
else if (a->tv_sec > b->tv_sec)
return 1;
if (a->tv_nsec < b->tv_nsec)
return -1;
else if (a->tv_nsec > b->tv_nsec)
return 1;
return 0;
}
struct timeval *
uc_ms2tv(struct timeval *dst, uint64_t ms)
{
dst->tv_sec = ms / 1000;
dst->tv_usec = ms % 1000 * 1000;
return dst;
}