diff --git a/lib/util.c b/lib/util.c index 7b87b73..0eff596 100644 --- a/lib/util.c +++ b/lib/util.c @@ -1,9 +1,11 @@ #include "util.h" #include +#include #include #include #include +#include #include #include "status.h" @@ -77,4 +79,22 @@ void name_thread(pthread_t thread, const char *name) if (ppthread_setname_np) { ppthread_setname_np(thread, name); } -} \ No newline at end of file +} + +size_t get_millis() +{ + size_t ms; // Milliseconds + size_t s; // Seconds + struct timespec spec; + + clock_gettime(CLOCK_REALTIME, &spec); + + s = spec.tv_sec; + ms = round(spec.tv_nsec / 1.0e6); // Convert nanoseconds to milliseconds + if (ms > 999) { + s++; + ms = 0; + } + + return (s * 1000) + ms; +} diff --git a/lib/util.h b/lib/util.h index 79ba4a3..2de8886 100644 --- a/lib/util.h +++ b/lib/util.h @@ -20,6 +20,7 @@ void force_interrupt(); void install_interrupt_handler(); void uninstall_interrupt_handler(); void name_thread(pthread_t thread, const char *name); +size_t get_millis(); uint16_t crc16(const void* data, size_t len);