add get_millis() to util

This commit is contained in:
itsmattkc 2024-10-27 18:22:20 -07:00
parent e140c1a58c
commit 717dcbc6cd
2 changed files with 22 additions and 1 deletions

View file

@ -1,9 +1,11 @@
#include "util.h"
#include <dlfcn.h>
#include <math.h>
#include <signal.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#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);
}
}
}
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;
}

View file

@ -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);