Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use std::chrono::time_point for efitimeus_t #16

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions mock/global.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
# test value just for unit tests

// test value just for unit tests
#define US_TO_NT_MULTIPLIER 100
6 changes: 3 additions & 3 deletions mock/lib-time-mocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
40 changes: 37 additions & 3 deletions util/include/rusefi/rusefi_time_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#pragma once

#include <chrono>
#include <cstdint>
#include <sys/types.h>

Expand All @@ -21,10 +22,14 @@
*/
using efitick_t = int64_t;

using efidurus_t = std::chrono::duration<int64_t, std::micro>;
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<std::chrono::steady_clock, efidurus_t>;
static_assert(sizeof(efitimeus_t) == 8);

// time in seconds
using efitimesec_t = time_t;
Expand Down Expand Up @@ -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<typename ms_type>
constexpr auto MS2US(ms_type ms_time) {
static_assert(
std::is_integral_v<ms_type> || std::is_floating_point_v<ms_type>,
"MS2US expects numeric type as parameter"
);
return ms_time * 1000;
}

template<typename ms_type>
constexpr auto US2MS(ms_type timeus) {
static_assert(
std::is_integral_v<ms_type> || std::is_floating_point_v<ms_type>,
"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<typename ms_type>
constexpr efitimeus_t USOF(ms_type us) {
static_assert(std::is_integral_v<ms_type>, "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();
}