14
14
15
15
using namespace std ::chrono_literals;
16
16
17
+ /* * Mockable clock in the context of tests, otherwise the system clock */
18
+ struct NodeClock : public std ::chrono::system_clock {
19
+ using time_point = std::chrono::time_point<NodeClock>;
20
+ /* * Return current system time or mocked time, if set */
21
+ static time_point now () noexcept ;
22
+ static std::time_t to_time_t (const time_point&) = delete; // unused
23
+ static time_point from_time_t (std::time_t ) = delete; // unused
24
+ };
25
+ using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
26
+
17
27
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
18
28
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
19
29
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
@@ -30,10 +40,10 @@ void UninterruptibleSleep(const std::chrono::microseconds& n);
30
40
* This helper is used to convert durations/time_points before passing them over an
31
41
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
32
42
*/
33
- template <typename Clock >
34
- constexpr int64_t count_seconds (std::chrono::time_point<Clock, std::chrono::seconds> t)
43
+ template <typename Duration, typename Timepoint >
44
+ constexpr auto TicksSinceEpoch (Timepoint t)
35
45
{
36
- return t .time_since_epoch ().count ();
46
+ return std::chrono::time_point_cast<Duration>(t) .time_since_epoch ().count ();
37
47
}
38
48
constexpr int64_t count_seconds (std::chrono::seconds t) { return t.count (); }
39
49
constexpr int64_t count_milliseconds (std::chrono::milliseconds t) { return t.count (); }
@@ -48,7 +58,11 @@ inline double CountSecondsDouble(SecondsDouble t) { return t.count(); }
48
58
49
59
/* *
50
60
* DEPRECATED
51
- * Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
61
+ * Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
62
+ * ClockType is
63
+ * - std::chrono::steady_clock for steady time
64
+ * - std::chrono::system_clock for system time
65
+ * - NodeClock for mockable system time
52
66
*/
53
67
int64_t GetTime ();
54
68
@@ -71,9 +85,6 @@ void SetMockTime(std::chrono::seconds mock_time_in);
71
85
/* * For testing */
72
86
std::chrono::seconds GetMockTime ();
73
87
74
- /* * Return system time (or mocked time, if set) */
75
- template <typename T>
76
- T GetTime ();
77
88
/* *
78
89
* Return the current time point cast to the given precicion. Only use this
79
90
* when an exact precicion is needed, otherwise use T::clock::now() directly.
@@ -83,6 +94,12 @@ T Now()
83
94
{
84
95
return std::chrono::time_point_cast<typename T::duration>(T::clock::now ());
85
96
}
97
+ /* * DEPRECATED, see GetTime */
98
+ template <typename T>
99
+ T GetTime ()
100
+ {
101
+ return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch ();
102
+ }
86
103
87
104
/* *
88
105
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date}
0 commit comments