From dc13bb827d6b7dd40d76ff6f7f3f06688e7d58a0 Mon Sep 17 00:00:00 2001 From: Tudor Brindus Date: Fri, 5 Jun 2020 17:44:30 -0400 Subject: util: fix and move `get_current_time_msec` into a util file 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/time.cc` so that it can be reused elsewhere without having to consider these pitfalls. --- util/meson.build | 1 + util/time.c | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 util/time.c (limited to 'util') diff --git a/util/meson.build b/util/meson.build index 8bdd7a74..14cd59c5 100644 --- a/util/meson.build +++ b/util/meson.build @@ -5,4 +5,5 @@ wlr_files += files( 'region.c', 'shm.c', 'signal.c', + 'time.c', ) diff --git a/util/time.c b/util/time.c new file mode 100644 index 00000000..b2333921 --- /dev/null +++ b/util/time.c @@ -0,0 +1,11 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include + +#include "util/time.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; +} -- cgit v1.2.3