]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
time: Add TIMEVAL_EARLIER and TIMEVAL_DIFF_SEC macros.
authorTodd Mortimer <todd@opennet.ca>
Mon, 30 Mar 2020 23:38:24 +0000 (23:38 +0000)
committerVictor Julien <vjulien@oisf.net>
Fri, 12 Nov 2021 06:36:56 +0000 (07:36 +0100)
Make it easy to compare 'struct timeval's and get their difference.

(cherry picked from commit 9fafc1031c0c9c11e5d98a286535b6b18af7cd3d)

src/util-time.h

index a8fb6db67282e6466990a8d9e44c359e9163d27c..501a2ebc3fe202eab2c7be12457ad98a5f41161f 100644 (file)
@@ -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);