From 9756ca0aebe3c0981a4dcab46264d6163060e7b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Wed, 25 Dec 2013 19:42:40 +0100 Subject: [PATCH] Create header tile for timeutils, rename tval.c to time.c --- include/ucore/time.h | 86 ++++++++++++++++++++++++++++++++++++++++++ src/{tval.c => time.c} | 48 ++++++++++++++++++++++- 2 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 include/ucore/time.h rename src/{tval.c => time.c} (50%) diff --git a/include/ucore/time.h b/include/ucore/time.h new file mode 100644 index 0000000..607bd44 --- /dev/null +++ b/include/ucore/time.h @@ -0,0 +1,86 @@ +#ifndef UC_TIME_H_ +#define UC_TIME_H_ + +#include +#include + +/** 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 diff --git a/src/tval.c b/src/time.c similarity index 50% rename from src/tval.c rename to src/time.c index 4cfab6e..c0a19a6 100644 --- a/src/tval.c +++ b/src/time.c @@ -1,4 +1,4 @@ -#include +#include "ucore/time.h" struct timeval * 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; } +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, unsigned long long ms) +uc_ms2tv(struct timeval *dst, uint64_t ms) { dst->tv_sec = ms / 1000; dst->tv_usec = ms % 1000 * 1000;