aboutsummaryrefslogtreecommitdiffstats
path: root/src/time
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-08-12 17:59:10 +0300
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2019-08-12 17:59:10 +0300
commit3fcd58c715993b3c2a42e21d41fece5d7d09243c (patch)
tree14793609fc8f94b2f9d8014dd8d554414cc6b53b /src/time
parent7c4f627ebb0e2d2f1922c4ec18032a3619ea5f1d (diff)
downloadlibrin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.tar.gz
librin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.tar.bz2
librin-3fcd58c715993b3c2a42e21d41fece5d7d09243c.zip
time: add timeval versions.
We now got functions to operate on both struct timespec and struct timeval. Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src/time')
-rw-r--r--src/time/time.c104
-rw-r--r--src/time/time_private.h1
2 files changed, 105 insertions, 0 deletions
diff --git a/src/time/time.c b/src/time/time.c
index d9f00b7..489196d 100644
--- a/src/time/time.c
+++ b/src/time/time.c
@@ -62,6 +62,47 @@ struct timespec rin_time_normalize(const struct timespec * const t)
return ret;
}
+struct timeval rin_timeval_add(const struct timeval * const a, const struct timeval * const b)
+{
+ struct timeval ret;
+
+ ret.tv_sec = a->tv_sec + b->tv_sec;
+ ret.tv_usec = a->tv_usec + b->tv_usec;
+
+ return rin_timeval_normalize(&ret);
+}
+
+struct timeval rin_timeval_sub(const struct timeval * const a, const struct timeval * const b)
+{
+ struct timeval ret;
+
+ ret.tv_sec = a->tv_sec - b->tv_sec;
+ ret.tv_usec = a->tv_usec - b->tv_usec;
+
+ return rin_timeval_normalize(&ret);
+}
+
+struct timeval rin_timeval_normalize(const struct timeval * const t)
+{
+ struct timeval ret;
+
+ ret = *t;
+
+ if (ret.tv_usec >= 0 && ret.tv_usec <= tv_usec_max) {
+ return ret;
+ }
+
+ if (ret.tv_usec > 0) {
+ ret.tv_sec += ret.tv_usec / (tv_usec_max + 1);
+ ret.tv_usec = ret.tv_usec % (tv_usec_max + 1);
+ } else {
+ ret.tv_sec += ret.tv_usec / (tv_usec_max + 1) - 1;
+ ret.tv_usec = (tv_usec_max + 1) - (-ret.tv_usec) % (tv_usec_max + 1);
+ }
+
+ return ret;
+}
+
long int rin_time_cmp(const struct timespec * const a, const struct timespec * const b)
{
if (a->tv_sec != b->tv_sec) {
@@ -71,6 +112,15 @@ long int rin_time_cmp(const struct timespec * const a, const struct timespec * c
}
}
+long int rin_timeval_cmp(const struct timeval * const a, const struct timeval * const b)
+{
+ if (a->tv_sec != b->tv_sec) {
+ return a->tv_sec - b->tv_sec;
+ } else {
+ return a->tv_usec - b->tv_usec;
+ }
+}
+
unsigned int rin_time_cmp_less(const struct timespec * const a, const struct timespec * const b)
{
if (a->tv_sec > b->tv_sec) {
@@ -124,3 +174,57 @@ unsigned int rin_time_cmp_nonequal(const struct timespec * const a, const struct
{
return a->tv_sec != b->tv_sec || a->tv_nsec != b->tv_nsec;
}
+
+unsigned int rin_timeval_cmp_less(const struct timeval * const a, const struct timeval * const b)
+{
+ if (a->tv_sec > b->tv_sec) {
+ return 0;
+ } else if (a->tv_sec < b->tv_sec) {
+ return 1;
+ } else {
+ return a->tv_usec < b->tv_usec;
+ }
+}
+
+unsigned int rin_timeval_cmp_more(const struct timeval * const a, const struct timeval * const b)
+{
+ if (a->tv_sec < b->tv_sec) {
+ return 0;
+ } else if (a->tv_sec > b->tv_sec) {
+ return 1;
+ } else {
+ return a->tv_usec > b->tv_usec;
+ }
+}
+
+unsigned int rin_timeval_cmp_lessequal(const struct timeval * const a, const struct timeval * const b)
+{
+ if (a->tv_sec > b->tv_sec) {
+ return 0;
+ } else if (a->tv_sec < b->tv_sec) {
+ return 1;
+ } else {
+ return a->tv_usec <= b->tv_usec;
+ }
+}
+
+unsigned int rin_timeval_cmp_moreequal(const struct timeval * const a, const struct timeval * const b)
+{
+ if (a->tv_sec < b->tv_sec) {
+ return 0;
+ } else if (a->tv_sec > b->tv_sec) {
+ return 1;
+ } else {
+ return a->tv_usec >= b->tv_usec;
+ }
+}
+
+unsigned int rin_timeval_cmp_equal(const struct timeval * const a, const struct timeval * const b)
+{
+ return a->tv_sec == b->tv_sec && a->tv_usec == b->tv_usec;
+}
+
+unsigned int rin_timeval_cmp_nonequal(const struct timeval * const a, const struct timeval * const b)
+{
+ return a->tv_sec != b->tv_sec || a->tv_usec != b->tv_usec;
+}
diff --git a/src/time/time_private.h b/src/time/time_private.h
index 4bb6099..097a987 100644
--- a/src/time/time_private.h
+++ b/src/time/time_private.h
@@ -22,5 +22,6 @@
#define LIBRIN_TIME_PRIVATE_INCLUDED
static const long tv_nsec_max = 999999999l;
+static const long tv_usec_max = 999999l;
#endif /* LIBRIN_TIME_PRIVATE_INCLUDED */