diff options
author | Dan McGee <dpmcgee@gmail.com> | 2011-11-16 01:36:31 -0600 |
---|---|---|
committer | Dan McGee <dpmcgee@gmail.com> | 2011-11-16 01:36:31 -0600 |
commit | a83a7d1021071db180f93ca24292cd48fffb1d3d (patch) | |
tree | 1894834c8065517d060a12311f6aa66e367cef40 /util.c | |
parent | 5b4ec0c382bfd394e9f35c94f424b43ba5d814f6 (diff) | |
download | onkyocontrol-a83a7d1021071db180f93ca24292cd48fffb1d3d.tar.gz onkyocontrol-a83a7d1021071db180f93ca24292cd48fffb1d3d.zip |
Consolidate timeval helper functions into util.c
And add a clear macro that saves a bit of verbosity.
Signed-off-by: Dan McGee <dpmcgee@gmail.com>
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 28 |
1 files changed, 28 insertions, 0 deletions
@@ -19,6 +19,7 @@ #include <stdlib.h> /* malloc */ #include <sys/stat.h> /* open */ +#include <sys/time.h> /* struct timeval */ #include <fcntl.h> /* open */ #include <unistd.h> /* close, read, write */ #include <errno.h> /* for errno refs */ @@ -88,4 +89,31 @@ unsigned long hash_sdbm(const char *str) return(hash); } +void timeval_diff(struct timeval * restrict a, + struct timeval * restrict b, struct timeval * restrict result) +{ + /* Calculate time difference as `a - b`. + * Make sure we end up with an in-range usecs value. */ + result->tv_sec = a->tv_sec - b->tv_sec; + result->tv_usec = a->tv_usec - b->tv_usec; + if(result->tv_usec < 0) { + result->tv_usec += 1000000; + result->tv_sec -= 1; + } +} + +struct timeval timeval_min(struct timeval *restrict a, + struct timeval * restrict b) +{ + if(a->tv_sec == 0 && a->tv_usec == 0) { + return(*b); + } if(a->tv_sec < b->tv_sec) { + return(*a); + } else if(a->tv_sec > b->tv_sec) { + return(*b); + } + /* getting here means seconds are equal */ + return(a->tv_usec < b->tv_usec ? *a : *b); +} + /* vim: set ts=4 sw=4 noet: */ |