mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-13 11:25:02 -05:00
Add time helpers for std::chrono::steady_clock
This commit is contained in:
parent
59ac8bacd5
commit
faa5c62967
2 changed files with 21 additions and 4 deletions
|
@ -1488,8 +1488,8 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
|
||||||
{
|
{
|
||||||
SetMockTime(111);
|
SetMockTime(111);
|
||||||
// Check that mock time does not change after a sleep
|
// Check that mock time does not change after a sleep
|
||||||
for (const auto& num_sleep : {0, 1}) {
|
for (const auto& num_sleep : {0ms, 1ms}) {
|
||||||
UninterruptibleSleep(std::chrono::milliseconds{num_sleep});
|
UninterruptibleSleep(num_sleep);
|
||||||
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
|
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
|
||||||
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
|
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
|
||||||
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
|
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
|
||||||
|
@ -1497,10 +1497,14 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
|
||||||
}
|
}
|
||||||
|
|
||||||
SetMockTime(0);
|
SetMockTime(0);
|
||||||
// Check that system time changes after a sleep
|
// Check that steady time and system time changes after a sleep
|
||||||
|
const auto steady_ms_0 = Now<SteadyMilliseconds>();
|
||||||
|
const auto steady_0 = std::chrono::steady_clock::now();
|
||||||
const auto ms_0 = GetTime<std::chrono::milliseconds>();
|
const auto ms_0 = GetTime<std::chrono::milliseconds>();
|
||||||
const auto us_0 = GetTime<std::chrono::microseconds>();
|
const auto us_0 = GetTime<std::chrono::microseconds>();
|
||||||
UninterruptibleSleep(std::chrono::milliseconds{1});
|
UninterruptibleSleep(1ms);
|
||||||
|
BOOST_CHECK(steady_ms_0 < Now<SteadyMilliseconds>());
|
||||||
|
BOOST_CHECK(steady_0 + 1ms <= std::chrono::steady_clock::now());
|
||||||
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
|
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
|
||||||
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
|
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,6 +14,10 @@
|
||||||
|
|
||||||
using namespace std::chrono_literals;
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
|
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
|
||||||
|
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
|
||||||
|
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
|
||||||
|
|
||||||
void UninterruptibleSleep(const std::chrono::microseconds& n);
|
void UninterruptibleSleep(const std::chrono::microseconds& n);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,6 +71,15 @@ std::chrono::seconds GetMockTime();
|
||||||
/** Return system time (or mocked time, if set) */
|
/** Return system time (or mocked time, if set) */
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T GetTime();
|
T GetTime();
|
||||||
|
/**
|
||||||
|
* Return the current time point cast to the given precicion. Only use this
|
||||||
|
* when an exact precicion is needed, otherwise use T::clock::now() directly.
|
||||||
|
*/
|
||||||
|
template <typename T>
|
||||||
|
T Now()
|
||||||
|
{
|
||||||
|
return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}
|
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}
|
||||||
|
|
Loading…
Add table
Reference in a new issue