Skip to content

Commit 6455284

Browse files
committed
use std::chrono::timepoint for efitimeus_t (see rusefi/rusefi#6409)
1 parent a61291f commit 6455284

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

mock/lib-time-mocks.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
static int timeNowUs = 0;
44

55
efitimeus_t getTimeNowUs() {
6-
return timeNowUs;
6+
return USOF(timeNowUs);
77
}
88

99
efitimesec_t getTimeNowS() {
10-
return getTimeNowUs() / 1000 / 1000;
10+
return COUNTOF(getTimeNowUs()) / 1000 / 1000;
1111
}
1212

1313
efitick_t getTimeNowNt() {
14-
return getTimeNowUs() * US_TO_NT_MULTIPLIER;
14+
return COUNTOF(getTimeNowUs()) * US_TO_NT_MULTIPLIER;
1515
}
1616

1717
void setTimeNowUs(int us) {

util/include/rusefi/rusefi_time_types.h

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#pragma once
44

5+
#include <chrono>
56
#include <cstdint>
67
#include <sys/types.h>
78

@@ -21,10 +22,14 @@
2122
*/
2223
using efitick_t = int64_t;
2324

25+
using efidurus_t = std::chrono::duration<int64_t, std::micro>;
26+
static_assert(sizeof(efidurus_t) == 8);
27+
2428
/**
2529
* 64 bit time in microseconds (1/1_000_000 of a second), since boot
2630
*/
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);
2833

2934
// time in seconds
3035
using efitimesec_t = time_t;
@@ -54,5 +59,34 @@ efitick_t getTimeNowNt();
5459
#define US_PER_SECOND_F 1000000.0
5560
#define US_PER_SECOND_LL 1000000LL
5661

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

Comments
 (0)