From: Todd Mortimer Date: Mon, 30 Mar 2020 23:38:24 +0000 (+0000) Subject: time: Add TIMEVAL_EARLIER and TIMEVAL_DIFF_SEC macros. X-Git-Tag: suricata-6.0.0-beta1~554 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9fafc1031c0c9c11e5d98a286535b6b18af7cd3d;p=thirdparty%2Fsuricata.git time: Add TIMEVAL_EARLIER and TIMEVAL_DIFF_SEC macros. Make it easy to compare 'struct timeval's and get their difference. --- diff --git a/src/util-time.h b/src/util-time.h index a8fb6db672..501a2ebc3f 100644 --- a/src/util-time.h +++ b/src/util-time.h @@ -33,6 +33,17 @@ void TimeGet(struct timeval *); /** \brief intialize a 'struct timespec' from a 'struct timeval'. */ #define FROM_TIMEVAL(timev) { .tv_sec = (timev).tv_sec, .tv_nsec = (timev).tv_usec * 1000 } +/** \brief compare two 'struct timeval' and return the difference in seconds */ +#define TIMEVAL_DIFF_SEC(tv_new, tv_old) \ + (uint64_t)((((uint64_t)(tv_new).tv_sec * 1000000 + (tv_new).tv_usec) - \ + ((uint64_t)(tv_old).tv_sec * 1000000 + (tv_old).tv_usec)) / \ + 1000000) + +/** \brief compare two 'struct timeval' and return if the first is earlier than the second */ +#define TIMEVAL_EARLIER(tv_first, tv_second) \ + (((tv_first).tv_sec < (tv_second).tv_sec) || \ + ((tv_first).tv_sec == (tv_second).tv_sec && (tv_first).tv_usec < (tv_second).tv_usec)) + #ifdef UNITTESTS void TimeSet(struct timeval *); void TimeSetToCurrentTime(void);