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
+86
View File
@@ -0,0 +1,86 @@
#ifndef UC_TIME_H_
#define UC_TIME_H_
#include <sys/time.h>
#include <stdint.h>
/** Add two struct timeval.
*
* @param dst where to store the result
* @param a first timeval to add
* @param b second timeval to add
* @return dst
*/
struct timeval *uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b);
/** Subtract two struct timeval.
*
* @param dst where to store the result
* @param a first timeval to subtract
* @param b second timeval to subtract
* @return dst
*/
struct timeval *uc_tvsub(struct timeval *dst, const struct timeval *a, const struct timeval *b);
/** Add two struct timevals.
*
* @param dst where to store the result
* @param a first timeval to add
* @param b second timeval to add
* @return dst
*/
int uc_tvcmp(const struct timeval *a, const struct timeval *b);
/** Add two struct timespec.
*
* @param dst where to store the result
* @param a first timespec to add
* @param b second timespec to add
* @return dst
*/
struct timespec *uc_tsadd(struct timespec *dst, const struct timespec *a, const struct timespec *b);
/** Subtract two struct timespec.
*
* @param dst where to store the result
* @param a first timespec to subtract
* @param b second timespec to subtract
* @return dst
*/
struct timespec *uc_tssub(struct timespec *dst, const struct timespec *a, const struct timespec *b);
/** Add two struct timespec.
*
* @param a first timespec to add
* @param b second timespec to add
* @return < 0 if a is less than b, 0 if a and be are equal, > 0 if a is greater than b
*/
int uc_tscmp(const struct timespec *a, const struct timespec *b);
static inline uint64_t uc_tv2msec(const struct timeval *t)
{
return t->tv_sec * 1000 + t->tv_usec / 1000;
}
static inline uint64_t uc_ts2msec(const struct timespec *t)
{
return t->tv_sec * 1000 + t->tv_nsec / 10000000;
}
static inline uint64_t uc_ts2nsec(const struct timespec *t)
{
return t->tv_sec * 1000000000ULL + t->tv_nsec;
}
/** Convert number of miliseconds to a timeval.
*
* @param dst where to store the result
* @param ms number of miliseconds
* @return dst
*/
struct timeval *uc_ms2tv(struct timeval *dst, uint64_t ms);
#endif
+46 -2
View File
@@ -1,4 +1,4 @@
#include <sys/time.h> #include "ucore/time.h"
struct timeval * struct timeval *
uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b) uc_tvadd(struct timeval *dst, const struct timeval *a, const struct timeval *b)
@@ -44,8 +44,52 @@ uc_tvcmp(const struct timeval *a, const struct timeval *b)
return 0; 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 * struct timeval *
uc_ms2tv(struct timeval *dst, unsigned long long ms) uc_ms2tv(struct timeval *dst, uint64_t ms)
{ {
dst->tv_sec = ms / 1000; dst->tv_sec = ms / 1000;
dst->tv_usec = ms % 1000 * 1000; dst->tv_usec = ms % 1000 * 1000;