/root/bitcoin/src/util/time.h
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #ifndef BITCOIN_UTIL_TIME_H |
7 | | #define BITCOIN_UTIL_TIME_H |
8 | | |
9 | | #include <chrono> // IWYU pragma: export |
10 | | #include <cstdint> |
11 | | #include <optional> |
12 | | #include <string> |
13 | | #include <string_view> |
14 | | |
15 | | using namespace std::chrono_literals; |
16 | | |
17 | | /// Version of the system clock that is mockable in the context of tests (via |
18 | | /// NodeClockContext or ::SetMockTime), otherwise the system clock. |
19 | | struct NodeClock : public std::chrono::system_clock { |
20 | | using time_point = std::chrono::time_point<NodeClock>; |
21 | | /** Return current system time or mocked time, if set */ |
22 | | static time_point now() noexcept; |
23 | | static std::time_t to_time_t(const time_point&) = delete; // unused |
24 | | static time_point from_time_t(std::time_t) = delete; // unused |
25 | | }; |
26 | | using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>; |
27 | | |
28 | | using SteadyClock = std::chrono::steady_clock; |
29 | | using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; |
30 | | using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; |
31 | | using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; |
32 | | |
33 | | using SystemClock = std::chrono::system_clock; |
34 | | |
35 | | /// Version of SteadyClock that is mockable in the context of tests (via |
36 | | /// SteadyClockContext, or Self::SetMockTime), otherwise the system steady |
37 | | /// clock. |
38 | | struct MockableSteadyClock : public std::chrono::steady_clock { |
39 | | using time_point = std::chrono::time_point<MockableSteadyClock>; |
40 | | |
41 | | using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>; |
42 | | static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1}; |
43 | | |
44 | | /** Return current system time or mocked time, if set */ |
45 | | static time_point now() noexcept; |
46 | | static std::time_t to_time_t(const time_point&) = delete; // unused |
47 | | static time_point from_time_t(std::time_t) = delete; // unused |
48 | | |
49 | | /** Set mock time for testing. |
50 | | * When mocking the steady clock, start at INITIAL_MOCK_TIME and add durations to elapse time as necessary |
51 | | * for testing. |
52 | | * To stop mocking, call ClearMockTime(). |
53 | | */ |
54 | | static void SetMockTime(mock_time_point::duration mock_time_in); |
55 | | |
56 | | /** Clear mock time, go back to system steady clock. */ |
57 | | static void ClearMockTime(); |
58 | | }; |
59 | | |
60 | | void UninterruptibleSleep(const std::chrono::microseconds& n); |
61 | | |
62 | | /** |
63 | | * Helper to count the seconds of a duration/time_point. |
64 | | * |
65 | | * All durations/time_points should be using std::chrono and calling this should generally |
66 | | * be avoided in code. Though, it is still preferred to an inline t.count() to |
67 | | * protect against a reliance on the exact type of t. |
68 | | * |
69 | | * This helper is used to convert durations/time_points before passing them over an |
70 | | * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) |
71 | | */ |
72 | | template <typename Dur1, typename Dur2> |
73 | | constexpr auto Ticks(Dur2 d) |
74 | 0 | { |
75 | 0 | return std::chrono::duration_cast<Dur1>(d).count(); |
76 | 0 | } Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1l> > >(std::chrono::duration<long, std::ratio<1l, 1l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1000000l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<double, std::ratio<1l, 1000l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<double, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1000l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<3600l, 1l> > >(std::chrono::duration<long, std::ratio<3600l, 1l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<3600l, 1l> >, std::chrono::duration<long, std::ratio<3600l, 1l> > >(std::chrono::duration<long, std::ratio<3600l, 1l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<double, std::ratio<3600l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1l> > >(std::chrono::duration<long, std::ratio<1l, 1l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<long, std::ratio<60l, 1l> >, std::chrono::duration<long, std::ratio<60l, 1l> > >(std::chrono::duration<long, std::ratio<60l, 1l> >) Unexecuted instantiation: auto Ticks<std::chrono::duration<double, std::ratio<1l, 1l> >, std::chrono::duration<long, std::ratio<1l, 1000000l> > >(std::chrono::duration<long, std::ratio<1l, 1000000l> >) |
77 | | |
78 | | template <typename Duration> |
79 | | constexpr int64_t TicksSeconds(Duration d) |
80 | 0 | { |
81 | 0 | return int64_t{Ticks<std::chrono::seconds>(d)}; |
82 | 0 | } |
83 | | template <typename Duration, typename Timepoint> |
84 | | constexpr auto TicksSinceEpoch(Timepoint t) |
85 | 0 | { |
86 | 0 | return Ticks<Duration>(t.time_since_epoch()); |
87 | 0 | } Unexecuted instantiation: auto TicksSinceEpoch<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1l> > > >(std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1l> > >) Unexecuted instantiation: auto TicksSinceEpoch<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1l> > > >(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1l> > >) Unexecuted instantiation: auto TicksSinceEpoch<std::chrono::duration<long, std::ratio<1l, 1l> >, std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > > >(std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >) Unexecuted instantiation: auto TicksSinceEpoch<std::chrono::duration<long, std::ratio<1l, 1000l> >, std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > > >(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long, std::ratio<1l, 1000000000l> > >) |
88 | 1 | constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } |
89 | 0 | constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } |
90 | 0 | constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } |
91 | | |
92 | | using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; |
93 | | using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; |
94 | | using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; |
95 | | |
96 | | /** |
97 | | * DEPRECATED |
98 | | * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. |
99 | | * ClockType is |
100 | | * - SteadyClock/std::chrono::steady_clock for steady time |
101 | | * - SystemClock/std::chrono::system_clock for system time |
102 | | * - NodeClock for mockable system time |
103 | | */ |
104 | | int64_t GetTime(); |
105 | | |
106 | | /** |
107 | | * DEPRECATED |
108 | | * Use SetMockTime with chrono type |
109 | | * |
110 | | * @param[in] nMockTimeIn Time in seconds. |
111 | | */ |
112 | | void SetMockTime(int64_t nMockTimeIn); |
113 | | |
114 | | /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ |
115 | | void SetMockTime(std::chrono::seconds mock_time_in); |
116 | | void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock); |
117 | | |
118 | | /** For testing */ |
119 | | std::chrono::seconds GetMockTime(); |
120 | | |
121 | | /** |
122 | | * Return the current time point cast to the given precision. Only use this |
123 | | * when an exact precision is needed, otherwise use T::clock::now() directly. |
124 | | */ |
125 | | template <typename T> |
126 | | T Now() |
127 | 2 | { |
128 | 2 | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |
129 | 2 | } Unexecuted instantiation: std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1l> > > Now<std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1l> > > >() std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1l> > > Now<std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1l> > > >() Line | Count | Source | 127 | 2 | { | 128 | 2 | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 129 | 2 | } |
Unexecuted instantiation: std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000l> > > Now<std::chrono::time_point<std::chrono::_V2::steady_clock, std::chrono::duration<long, std::ratio<1l, 1000l> > > >() Unexecuted instantiation: std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000l> > > Now<std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000l> > > >() Unexecuted instantiation: std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000000l> > > Now<std::chrono::time_point<NodeClock, std::chrono::duration<long, std::ratio<1l, 1000000l> > > >() |
130 | | /** DEPRECATED, see GetTime */ |
131 | | template <typename T> |
132 | | T GetTime() |
133 | 0 | { |
134 | 0 | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); |
135 | 0 | } Unexecuted instantiation: std::chrono::duration<long, std::ratio<1l, 1000l> > GetTime<std::chrono::duration<long, std::ratio<1l, 1000l> > >() Unexecuted instantiation: std::chrono::duration<long, std::ratio<1l, 1l> > GetTime<std::chrono::duration<long, std::ratio<1l, 1l> > >() Unexecuted instantiation: std::chrono::duration<long, std::ratio<1l, 1000000l> > GetTime<std::chrono::duration<long, std::ratio<1l, 1000000l> > >() |
136 | | |
137 | | /** |
138 | | * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} |
139 | | * helper functions if possible. |
140 | | */ |
141 | | std::string FormatISO8601DateTime(int64_t nTime); |
142 | | std::string FormatISO8601Date(int64_t nTime); |
143 | | std::optional<int64_t> ParseISO8601DateTime(std::string_view str); |
144 | | |
145 | | /** |
146 | | * RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 |
147 | | * Used in HTTP/1.1 responses |
148 | | */ |
149 | | std::string FormatRFC1123DateTime(int64_t nTime); |
150 | | |
151 | | /** |
152 | | * Convert milliseconds to a struct timeval for e.g. select. |
153 | | */ |
154 | | struct timeval MillisToTimeval(int64_t nTimeout); |
155 | | |
156 | | /** |
157 | | * Convert milliseconds to a struct timeval for e.g. select. |
158 | | */ |
159 | | struct timeval MillisToTimeval(std::chrono::milliseconds ms); |
160 | | |
161 | | #endif // BITCOIN_UTIL_TIME_H |