From d7900c6e5e82406c1ed6a6df2ff3d1896149deff Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Fri, 5 Jun 2020 17:12:31 -0400 Subject: common/util: fix `get_current_time_msec` returning microseconds This commit makes `get_current_time_msec` correctly return milliseconds as opposed to microseconds. It also considers the value of `tv_sec`, so we don't lose occasionally go back in time by one second. Finally, the function is moved into `util.c` so that it can be reused elsewhere without having to consider these pitfalls. --- common/util.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'common') diff --git a/common/util.c b/common/util.c index c7ef2ac4..cb142a5e 100644 --- a/common/util.c +++ b/common/util.c @@ -1,8 +1,8 @@ #define _POSIX_C_SOURCE 200809L #include -#include #include #include +#include #include #include #include @@ -10,6 +10,12 @@ #include "log.h" #include "util.h" +uint32_t get_current_time_msec(void) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + return now.tv_sec * 1000 + now.tv_nsec / 1000000; +} + int wrap(int i, int max) { return ((i % max) + max) % max; } -- cgit v1.2.3