From 90183beeb914590a2ea4eff0242b813562d2d641 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Sat, 4 Feb 2023 14:37:31 -0800 Subject: Add time_point_to_ns(). --- timer/src/timer.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'timer/src/timer.c') diff --git a/timer/src/timer.c b/timer/src/timer.c index 2ee8ef5..0c33e51 100644 --- a/timer/src/timer.c +++ b/timer/src/timer.c @@ -32,17 +32,17 @@ Timer timer_make(void) { } void timer_start(Timer* timer) { - timer->start_time = time_now(); - timer->last_tick = timer->start_time; + timer->start_time = time_now(); + timer->last_tick = timer->start_time; timer->running_time = 0; - timer->delta_time = 0; + timer->delta_time = 0; } void timer_tick(Timer* timer) { const time_point this_tick = time_now(); - timer->running_time = time_diff(timer->start_time, this_tick); - timer->delta_time = time_diff(timer->last_tick, this_tick); - timer->last_tick = this_tick; + timer->running_time = time_diff(timer->start_time, this_tick); + timer->delta_time = time_diff(timer->last_tick, this_tick); + timer->last_tick = this_tick; } time_point time_now(void) { @@ -82,14 +82,22 @@ time_delta sec_to_time_delta(double seconds) { #endif } +uint64_t time_point_to_ns(time_point t) { +#ifdef _WIN32 + return (uint64_t)((double)t * seconds_per_count * 1.0e+9); +#else + return (uint64_t)t.tv_sec * 1e+9 + (uint64_t)t.tv_nsec; +#endif +} + void time_sleep(time_delta dt) { #ifdef _WIN32 const int64_t ms = dt / microseconds; Sleep((DWORD)(ms)); #else - const int64_t sec = dt / nanoseconds; + const int64_t sec = dt / nanoseconds; struct timespec ts; - ts.tv_sec = (long)sec; + ts.tv_sec = (long)sec; ts.tv_nsec = (long)(dt % nanoseconds); nanosleep(&ts, NULL); #endif -- cgit v1.2.3