diff options
author | 3gg <3gg@shellblade.net> | 2023-02-04 14:37:31 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-02-04 14:37:31 -0800 |
commit | 90183beeb914590a2ea4eff0242b813562d2d641 (patch) | |
tree | 612c486eecb968d947058312fec4e5c318f1a743 /timer/src/timer.c | |
parent | 20169ed6d5551d2fee59d9e4bc8caa73a8b8da7c (diff) |
Add time_point_to_ns().
Diffstat (limited to 'timer/src/timer.c')
-rw-r--r-- | timer/src/timer.c | 24 |
1 files changed, 16 insertions, 8 deletions
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) { | |||
32 | } | 32 | } |
33 | 33 | ||
34 | void timer_start(Timer* timer) { | 34 | void timer_start(Timer* timer) { |
35 | timer->start_time = time_now(); | 35 | timer->start_time = time_now(); |
36 | timer->last_tick = timer->start_time; | 36 | timer->last_tick = timer->start_time; |
37 | timer->running_time = 0; | 37 | timer->running_time = 0; |
38 | timer->delta_time = 0; | 38 | timer->delta_time = 0; |
39 | } | 39 | } |
40 | 40 | ||
41 | void timer_tick(Timer* timer) { | 41 | void timer_tick(Timer* timer) { |
42 | const time_point this_tick = time_now(); | 42 | const time_point this_tick = time_now(); |
43 | timer->running_time = time_diff(timer->start_time, this_tick); | 43 | timer->running_time = time_diff(timer->start_time, this_tick); |
44 | timer->delta_time = time_diff(timer->last_tick, this_tick); | 44 | timer->delta_time = time_diff(timer->last_tick, this_tick); |
45 | timer->last_tick = this_tick; | 45 | timer->last_tick = this_tick; |
46 | } | 46 | } |
47 | 47 | ||
48 | time_point time_now(void) { | 48 | time_point time_now(void) { |
@@ -82,14 +82,22 @@ time_delta sec_to_time_delta(double seconds) { | |||
82 | #endif | 82 | #endif |
83 | } | 83 | } |
84 | 84 | ||
85 | uint64_t time_point_to_ns(time_point t) { | ||
86 | #ifdef _WIN32 | ||
87 | return (uint64_t)((double)t * seconds_per_count * 1.0e+9); | ||
88 | #else | ||
89 | return (uint64_t)t.tv_sec * 1e+9 + (uint64_t)t.tv_nsec; | ||
90 | #endif | ||
91 | } | ||
92 | |||
85 | void time_sleep(time_delta dt) { | 93 | void time_sleep(time_delta dt) { |
86 | #ifdef _WIN32 | 94 | #ifdef _WIN32 |
87 | const int64_t ms = dt / microseconds; | 95 | const int64_t ms = dt / microseconds; |
88 | Sleep((DWORD)(ms)); | 96 | Sleep((DWORD)(ms)); |
89 | #else | 97 | #else |
90 | const int64_t sec = dt / nanoseconds; | 98 | const int64_t sec = dt / nanoseconds; |
91 | struct timespec ts; | 99 | struct timespec ts; |
92 | ts.tv_sec = (long)sec; | 100 | ts.tv_sec = (long)sec; |
93 | ts.tv_nsec = (long)(dt % nanoseconds); | 101 | ts.tv_nsec = (long)(dt % nanoseconds); |
94 | nanosleep(&ts, NULL); | 102 | nanosleep(&ts, NULL); |
95 | #endif | 103 | #endif |