util: add helper to get time in nanoseconds.

Signed-off-by: Sergio Gómez <sergio.g.delreal@gmail.com>
This commit is contained in:
Sergio Gómez 2025-12-01 17:23:46 -05:00
parent abf80b529e
commit 0e070e93d8
2 changed files with 11 additions and 0 deletions

View file

@ -6,6 +6,11 @@
static const long NSEC_PER_SEC = 1000000000; static const long NSEC_PER_SEC = 1000000000;
/**
* Get the current time, in nanoseconds.
*/
int64_t get_current_time_nsec(void);
/** /**
* Get the current time, in milliseconds. * Get the current time, in milliseconds.
*/ */

View file

@ -22,6 +22,12 @@ int64_t get_current_time_msec(void) {
return timespec_to_msec(&now); return timespec_to_msec(&now);
} }
int64_t get_current_time_nsec(void) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
return timespec_to_nsec(&now);
}
void timespec_sub(struct timespec *r, const struct timespec *a, void timespec_sub(struct timespec *r, const struct timespec *a,
const struct timespec *b) { const struct timespec *b) {
r->tv_sec = a->tv_sec - b->tv_sec; r->tv_sec = a->tv_sec - b->tv_sec;