|
2 | 2 |
|
3 | 3 | #pragma once |
4 | 4 |
|
| 5 | +#include <chrono> |
5 | 6 | #include <cstdint> |
6 | 7 | #include <sys/types.h> |
7 | 8 |
|
|
21 | 22 | */ |
22 | 23 | using efitick_t = int64_t; |
23 | 24 |
|
| 25 | +using efidurus_t = std::chrono::duration<int64_t, std::micro>; |
| 26 | +static_assert(sizeof(efidurus_t) == 8); |
| 27 | + |
24 | 28 | /** |
25 | 29 | * 64 bit time in microseconds (1/1_000_000 of a second), since boot |
26 | 30 | */ |
27 | | -using efitimeus_t = int64_t; |
| 31 | +using efitimeus_t = std::chrono::time_point<std::chrono::steady_clock, efidurus_t>; |
| 32 | +static_assert(sizeof(efitimeus_t) == 8); |
28 | 33 |
|
29 | 34 | // time in seconds |
30 | 35 | using efitimesec_t = time_t; |
@@ -54,5 +59,34 @@ efitick_t getTimeNowNt(); |
54 | 59 | #define US_PER_SECOND_F 1000000.0 |
55 | 60 | #define US_PER_SECOND_LL 1000000LL |
56 | 61 |
|
57 | | -#define MS2US(MS_TIME) ((MS_TIME) * 1000) |
58 | | -#define US2MS(US_TIME) ((US_TIME) / 1000) |
| 62 | +template<typename ms_type> |
| 63 | +constexpr auto MS2US(ms_type ms_time) { |
| 64 | + static_assert( |
| 65 | + std::is_integral_v<ms_type> || std::is_floating_point_v<ms_type>, |
| 66 | + "MS2US expects numeric type as parameter" |
| 67 | + ); |
| 68 | + return ms_time * 1000; |
| 69 | +} |
| 70 | + |
| 71 | +template<typename ms_type> |
| 72 | +constexpr auto US2MS(ms_type timeus) { |
| 73 | + static_assert( |
| 74 | + std::is_integral_v<ms_type> || std::is_floating_point_v<ms_type>, |
| 75 | + "US2MS expects numeric type or efitimeus_t as parameter" |
| 76 | + ); |
| 77 | + return timeus / 1000; |
| 78 | +} |
| 79 | + |
| 80 | +constexpr efitimeus_t::rep US2MS(const efitimeus_t& timeus) { |
| 81 | + return timeus.time_since_epoch().count() / 1000; |
| 82 | +} |
| 83 | + |
| 84 | +template<typename ms_type> |
| 85 | +constexpr efitimeus_t USOF(ms_type us) { |
| 86 | + static_assert(std::is_integral_v<ms_type>, "US2MS expects integral type as parameter"); |
| 87 | + return efitimeus_t(efidurus_t(us)); |
| 88 | +} |
| 89 | + |
| 90 | +constexpr efitimeus_t::rep COUNTOF(const efitimeus_t& us) { |
| 91 | + return us.time_since_epoch().count(); |
| 92 | +} |
0 commit comments