Warning: That file was not part of the compilation database. It may have many parsing errors.
| 1 | /* @title: Generic Monotonic Timestamp */ |
|---|---|
| 2 | #pragma once |
| 3 | #include <time/time.h> |
| 4 | #include <types/types.h> |
| 5 | |
| 6 | struct mono_time { |
| 7 | time_ns_t internal; /* We store the time in nanoseconds internally, however |
| 8 | * NOTE: DO NOT use this as a nanosecond counter, the |
| 9 | * idea of this struct is that it is a generic |
| 10 | * timestamp for easier cross-unit conversions */ |
| 11 | }; |
| 12 | |
| 13 | static inline time_s_t mono_time_to_s(struct mono_time ts) { |
| 14 | return (time_s_t) NS_TO_SECONDS(ts.internal); |
| 15 | } |
| 16 | |
| 17 | static inline time_ms_t mono_time_to_ms(struct mono_time ts) { |
| 18 | return (time_ms_t) NS_TO_MS(ts.internal); |
| 19 | } |
| 20 | |
| 21 | static inline time_us_t mono_time_to_us(struct mono_time ts) { |
| 22 | return (time_us_t) NS_TO_US(ts.internal); |
| 23 | } |
| 24 | |
| 25 | static inline time_ns_t mono_time_to_ns(struct mono_time ts) { |
| 26 | return ts.internal; |
| 27 | } |
| 28 | |
| 29 | static inline struct mono_time mono_time_from_s(time_s_t s) { |
| 30 | return (struct mono_time) {.internal = SECONDS_TO_NS(s)}; |
| 31 | } |
| 32 | |
| 33 | static inline struct mono_time mono_time_from_ms(time_ms_t ms) { |
| 34 | return (struct mono_time) {.internal = MS_TO_NS(ms)}; |
| 35 | } |
| 36 | |
| 37 | static inline struct mono_time mono_time_from_us(time_us_t us) { |
| 38 | return (struct mono_time) {.internal = US_TO_NS(us)}; |
| 39 | } |
| 40 | |
| 41 | static inline struct mono_time mono_time_from_ns(time_ns_t ns) { |
| 42 | return (struct mono_time) {.internal = ns}; |
| 43 | } |
| 44 | |
| 45 | /* Returns the old value */ |
| 46 | static inline time_s_t mono_time_set_s(struct mono_time *ts, time_s_t s) { |
| 47 | time_s_t old = mono_time_to_s(ts: *ts); |
| 48 | ts->internal = SECONDS_TO_NS(s); |
| 49 | return old; |
| 50 | } |
| 51 | |
| 52 | static inline time_ms_t mono_time_set_ms(struct mono_time *ts, time_ms_t ms) { |
| 53 | time_ms_t old = mono_time_to_ms(ts: *ts); |
| 54 | ts->internal = MS_TO_NS(ms); |
| 55 | return old; |
| 56 | } |
| 57 | |
| 58 | static inline time_us_t mono_time_set_us(struct mono_time *ts, time_us_t us) { |
| 59 | time_us_t old = mono_time_to_us(ts: *ts); |
| 60 | ts->internal = US_TO_NS(us); |
| 61 | return old; |
| 62 | } |
| 63 | |
| 64 | static inline time_ns_t mono_time_set_ns(struct mono_time *ts, time_ns_t ns) { |
| 65 | time_ns_t old = mono_time_to_ns(ts: *ts); |
| 66 | ts->internal = ns; |
| 67 | return old; |
| 68 | } |
| 69 |
Warning: That file was not part of the compilation database. It may have many parsing errors.