Skip to content

Commit faa5c62

Browse files
author
MarcoFalke
committed
Add time helpers for std::chrono::steady_clock
1 parent 59ac8ba commit faa5c62

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/test/util_tests.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,19 +1488,23 @@ BOOST_AUTO_TEST_CASE(util_time_GetTime)
14881488
{
14891489
SetMockTime(111);
14901490
// Check that mock time does not change after a sleep
1491-
for (const auto& num_sleep : {0, 1}) {
1492-
UninterruptibleSleep(std::chrono::milliseconds{num_sleep});
1491+
for (const auto& num_sleep : {0ms, 1ms}) {
1492+
UninterruptibleSleep(num_sleep);
14931493
BOOST_CHECK_EQUAL(111, GetTime()); // Deprecated time getter
14941494
BOOST_CHECK_EQUAL(111, GetTime<std::chrono::seconds>().count());
14951495
BOOST_CHECK_EQUAL(111000, GetTime<std::chrono::milliseconds>().count());
14961496
BOOST_CHECK_EQUAL(111000000, GetTime<std::chrono::microseconds>().count());
14971497
}
14981498

14991499
SetMockTime(0);
1500-
// Check that system time changes after a sleep
1500+
// Check that steady time and system time changes after a sleep
1501+
const auto steady_ms_0 = Now<SteadyMilliseconds>();
1502+
const auto steady_0 = std::chrono::steady_clock::now();
15011503
const auto ms_0 = GetTime<std::chrono::milliseconds>();
15021504
const auto us_0 = GetTime<std::chrono::microseconds>();
1503-
UninterruptibleSleep(std::chrono::milliseconds{1});
1505+
UninterruptibleSleep(1ms);
1506+
BOOST_CHECK(steady_ms_0 < Now<SteadyMilliseconds>());
1507+
BOOST_CHECK(steady_0 + 1ms <= std::chrono::steady_clock::now());
15041508
BOOST_CHECK(ms_0 < GetTime<std::chrono::milliseconds>());
15051509
BOOST_CHECK(us_0 < GetTime<std::chrono::microseconds>());
15061510
}

src/util/time.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414

1515
using namespace std::chrono_literals;
1616

17+
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
18+
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
19+
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
20+
1721
void UninterruptibleSleep(const std::chrono::microseconds& n);
1822

1923
/**
@@ -67,6 +71,15 @@ std::chrono::seconds GetMockTime();
6771
/** Return system time (or mocked time, if set) */
6872
template <typename T>
6973
T GetTime();
74+
/**
75+
* Return the current time point cast to the given precicion. Only use this
76+
* when an exact precicion is needed, otherwise use T::clock::now() directly.
77+
*/
78+
template <typename T>
79+
T Now()
80+
{
81+
return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
82+
}
7083

7184
/**
7285
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}

0 commit comments

Comments
 (0)