diff --git a/mock/global.h b/mock/global.h index 0b61c7f..67a183a 100644 --- a/mock/global.h +++ b/mock/global.h @@ -1,3 +1,2 @@ -# test value just for unit tests - +// test value just for unit tests #define US_TO_NT_MULTIPLIER 100 \ No newline at end of file diff --git a/mock/lib-time-mocks.cpp b/mock/lib-time-mocks.cpp index 06b2ef0..2566fde 100644 --- a/mock/lib-time-mocks.cpp +++ b/mock/lib-time-mocks.cpp @@ -3,15 +3,15 @@ static int timeNowUs = 0; efitimeus_t getTimeNowUs() { - return timeNowUs; + return USOF(timeNowUs); } efitimesec_t getTimeNowS() { - return getTimeNowUs() / 1000 / 1000; + return COUNTOF(getTimeNowUs()) / 1000 / 1000; } efitick_t getTimeNowNt() { - return getTimeNowUs() * US_TO_NT_MULTIPLIER; + return COUNTOF(getTimeNowUs()) * US_TO_NT_MULTIPLIER; } void setTimeNowUs(int us) { diff --git a/util/include/rusefi/rusefi_time_types.h b/util/include/rusefi/rusefi_time_types.h index 2406f02..ab85774 100644 --- a/util/include/rusefi/rusefi_time_types.h +++ b/util/include/rusefi/rusefi_time_types.h @@ -2,6 +2,7 @@ #pragma once +#include #include #include @@ -21,10 +22,14 @@ */ using efitick_t = int64_t; +using efidurus_t = std::chrono::duration; +static_assert(sizeof(efidurus_t) == 8); + /** * 64 bit time in microseconds (1/1_000_000 of a second), since boot */ -using efitimeus_t = int64_t; +using efitimeus_t = std::chrono::time_point; +static_assert(sizeof(efitimeus_t) == 8); // time in seconds using efitimesec_t = time_t; @@ -54,5 +59,34 @@ efitick_t getTimeNowNt(); #define US_PER_SECOND_F 1000000.0 #define US_PER_SECOND_LL 1000000LL -#define MS2US(MS_TIME) ((MS_TIME) * 1000) -#define US2MS(US_TIME) ((US_TIME) / 1000) +template +constexpr auto MS2US(ms_type ms_time) { + static_assert( + std::is_integral_v || std::is_floating_point_v, + "MS2US expects numeric type as parameter" + ); + return ms_time * 1000; +} + +template +constexpr auto US2MS(ms_type timeus) { + static_assert( + std::is_integral_v || std::is_floating_point_v, + "US2MS expects numeric type or efitimeus_t as parameter" + ); + return timeus / 1000; +} + +constexpr efitimeus_t::rep US2MS(const efitimeus_t& timeus) { + return timeus.time_since_epoch().count() / 1000; +} + +template +constexpr efitimeus_t USOF(ms_type us) { + static_assert(std::is_integral_v, "US2MS expects integral type as parameter"); + return efitimeus_t(efidurus_t(us)); +} + +constexpr efitimeus_t::rep COUNTOF(const efitimeus_t& us) { + return us.time_since_epoch().count(); +}