/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 | | // The `util/time.h` header is designed to be a drop-in replacement for `chrono`. |
10 | | #include <chrono> // IWYU pragma: export |
11 | | #include <cstdint> |
12 | | #include <ctime> |
13 | | #include <optional> |
14 | | #include <string> |
15 | | #include <string_view> |
16 | | |
17 | | #ifdef WIN32 |
18 | | #include <winsock2.h> |
19 | | #else |
20 | | #include <sys/time.h> |
21 | | #endif |
22 | | |
23 | | using namespace std::chrono_literals; |
24 | | |
25 | | /// Version of the system clock that is mockable in the context of tests (via |
26 | | /// NodeClockContext or ::SetMockTime), otherwise the system clock. |
27 | | struct NodeClock : public std::chrono::system_clock { |
28 | | using time_point = std::chrono::time_point<NodeClock>; |
29 | | /** Return current system time or mocked time, if set */ |
30 | | static time_point now() noexcept; |
31 | | static std::time_t to_time_t(const time_point&) = delete; // unused |
32 | | static time_point from_time_t(std::time_t) = delete; // unused |
33 | | }; |
34 | | using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>; |
35 | | |
36 | | using SteadyClock = std::chrono::steady_clock; |
37 | | using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; |
38 | | using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; |
39 | | using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; |
40 | | |
41 | | using SystemClock = std::chrono::system_clock; |
42 | | |
43 | | /// Version of SteadyClock that is mockable in the context of tests (via |
44 | | /// SteadyClockContext, or Self::SetMockTime), otherwise the system steady |
45 | | /// clock. |
46 | | struct MockableSteadyClock : public std::chrono::steady_clock { |
47 | | using time_point = std::chrono::time_point<MockableSteadyClock>; |
48 | | |
49 | | using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>; |
50 | | static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1}; |
51 | | |
52 | | /** Return current system time or mocked time, if set */ |
53 | | static time_point now() noexcept; |
54 | | static std::time_t to_time_t(const time_point&) = delete; // unused |
55 | | static time_point from_time_t(std::time_t) = delete; // unused |
56 | | |
57 | | /** Set mock time for testing. |
58 | | * When mocking the steady clock, start at INITIAL_MOCK_TIME and add durations to elapse time as necessary |
59 | | * for testing. |
60 | | * To stop mocking, call ClearMockTime(). |
61 | | */ |
62 | | static void SetMockTime(mock_time_point::duration mock_time_in); |
63 | | |
64 | | /** Clear mock time, go back to system steady clock. */ |
65 | | static void ClearMockTime(); |
66 | | }; |
67 | | |
68 | | void UninterruptibleSleep(const std::chrono::microseconds& n); |
69 | | |
70 | | /** |
71 | | * Helper to count the seconds of a duration/time_point. |
72 | | * |
73 | | * All durations/time_points should be using std::chrono and calling this should generally |
74 | | * be avoided in code. Though, it is still preferred to an inline t.count() to |
75 | | * protect against a reliance on the exact type of t. |
76 | | * |
77 | | * This helper is used to convert durations/time_points before passing them over an |
78 | | * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) |
79 | | */ |
80 | | template <typename Dur1, typename Dur2> |
81 | | constexpr auto Ticks(Dur2 d) |
82 | 32.1M | { |
83 | 32.1M | return std::chrono::duration_cast<Dur1>(d).count(); |
84 | 32.1M | } _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEES4_EDaT0_ Line | Count | Source | 82 | 19.6M | { | 83 | 19.6M | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 19.6M | } |
_Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1000000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Line | Count | Source | 82 | 3.36M | { | 83 | 3.36M | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 3.36M | } |
_Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Line | Count | Source | 82 | 6.41M | { | 83 | 6.41M | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 6.41M | } |
_Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Line | Count | Source | 82 | 1.16M | { | 83 | 1.16M | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 1.16M | } |
_Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Line | Count | Source | 82 | 578 | { | 83 | 578 | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 578 | } |
_Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Line | Count | Source | 82 | 1.20M | { | 83 | 1.20M | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 1.20M | } |
_Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS1_IlS2_ILl3600ELl1EEEEEDaT0_ Line | Count | Source | 82 | 347k | { | 83 | 347k | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 347k | } |
_Z5TicksINSt6chrono8durationIlSt5ratioILl3600ELl1EEEES4_EDaT0_ Line | Count | Source | 82 | 1.14k | { | 83 | 1.14k | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 1.14k | } |
Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl3600ELl1EEEENS1_IlS2_ILl1ELl1EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000EEEEEDaT0_ _Z5TicksINSt6chrono8durationIlSt5ratioILl60ELl1EEEES4_EDaT0_ Line | Count | Source | 82 | 41.9k | { | 83 | 41.9k | return std::chrono::duration_cast<Dur1>(d).count(); | 84 | 41.9k | } |
Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000EEEEEDaT0_ |
85 | | |
86 | | template <typename Duration> |
87 | | constexpr int64_t TicksSeconds(Duration d) |
88 | 2 | { |
89 | 2 | return int64_t{Ticks<std::chrono::seconds>(d)}; |
90 | 2 | } |
91 | | template <typename Duration, typename Timepoint> |
92 | | constexpr auto TicksSinceEpoch(Timepoint t) |
93 | 20.8M | { |
94 | 20.8M | return Ticks<Duration>(t.time_since_epoch()); |
95 | 20.8M | } _Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointI9NodeClockS4_EEEDaT0_ Line | Count | Source | 93 | 16.2M | { | 94 | 16.2M | return Ticks<Duration>(t.time_since_epoch()); | 95 | 16.2M | } |
_Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointINS0_3_V212system_clockES4_EEEDaT0_ Line | Count | Source | 93 | 3.36M | { | 94 | 3.36M | return Ticks<Duration>(t.time_since_epoch()); | 95 | 3.36M | } |
_Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointI9NodeClockNS1_IlS2_ILl1ELl1000000000EEEEEEEDaT0_ Line | Count | Source | 93 | 1.19M | { | 94 | 1.19M | return Ticks<Duration>(t.time_since_epoch()); | 95 | 1.19M | } |
_Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1000EEEENS0_10time_pointINS0_3_V212system_clockENS1_IlS2_ILl1ELl1000000000EEEEEEEDaT0_ Line | Count | Source | 93 | 1 | { | 94 | 1 | return Ticks<Duration>(t.time_since_epoch()); | 95 | 1 | } |
|
96 | 3.69M | constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } |
97 | 0 | constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } |
98 | 0 | constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } |
99 | | |
100 | | using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; |
101 | | using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; |
102 | | using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; |
103 | | |
104 | | /** |
105 | | * DEPRECATED |
106 | | * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. |
107 | | * ClockType is |
108 | | * - SteadyClock/std::chrono::steady_clock for steady time |
109 | | * - SystemClock/std::chrono::system_clock for system time |
110 | | * - NodeClock for mockable system time |
111 | | */ |
112 | | int64_t GetTime(); |
113 | | |
114 | | /** |
115 | | * DEPRECATED |
116 | | * Use SetMockTime with chrono type |
117 | | * |
118 | | * @param[in] nMockTimeIn Time in seconds. |
119 | | */ |
120 | | void SetMockTime(int64_t nMockTimeIn); |
121 | | |
122 | | /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ |
123 | | void SetMockTime(std::chrono::seconds mock_time_in); |
124 | | void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock); |
125 | | |
126 | | /** For testing */ |
127 | | std::chrono::seconds GetMockTime(); |
128 | | |
129 | | /** |
130 | | * Return the current time point cast to the given precision. Only use this |
131 | | * when an exact precision is needed, otherwise use T::clock::now() directly. |
132 | | */ |
133 | | template <typename T> |
134 | | T Now() |
135 | 14.5M | { |
136 | 14.5M | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |
137 | 14.5M | } _Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1EEEEEEET_v Line | Count | Source | 135 | 12.8M | { | 136 | 12.8M | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 137 | 12.8M | } |
_Z3NowINSt6chrono10time_pointINS0_3_V212steady_clockENS0_8durationIlSt5ratioILl1ELl1EEEEEEET_v Line | Count | Source | 135 | 454 | { | 136 | 454 | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 137 | 454 | } |
_Z3NowINSt6chrono10time_pointINS0_3_V212steady_clockENS0_8durationIlSt5ratioILl1ELl1000EEEEEEET_v Line | Count | Source | 135 | 29 | { | 136 | 29 | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 137 | 29 | } |
_Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1000EEEEEEET_v Line | Count | Source | 135 | 26.4k | { | 136 | 26.4k | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 137 | 26.4k | } |
_Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1000000EEEEEEET_v Line | Count | Source | 135 | 1.74M | { | 136 | 1.74M | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 137 | 1.74M | } |
|
138 | | /** DEPRECATED, see GetTime */ |
139 | | template <typename T> |
140 | | T GetTime() |
141 | 6.72M | { |
142 | 6.72M | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); |
143 | 6.72M | } _Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1000EEEEET_v Line | Count | Source | 141 | 26.4k | { | 142 | 26.4k | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); | 143 | 26.4k | } |
_Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1EEEEET_v Line | Count | Source | 141 | 4.94M | { | 142 | 4.94M | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); | 143 | 4.94M | } |
_Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1000000EEEEET_v Line | Count | Source | 141 | 1.74M | { | 142 | 1.74M | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); | 143 | 1.74M | } |
|
144 | | |
145 | | /** |
146 | | * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} |
147 | | * helper functions if possible. |
148 | | */ |
149 | | std::string FormatISO8601DateTime(int64_t nTime); |
150 | | std::string FormatISO8601Date(int64_t nTime); |
151 | | std::optional<int64_t> ParseISO8601DateTime(std::string_view str); |
152 | | |
153 | | /** |
154 | | * RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 |
155 | | * Used in HTTP/1.1 responses |
156 | | */ |
157 | | std::string FormatRFC1123DateTime(int64_t nTime); |
158 | | |
159 | | /** |
160 | | * Convert milliseconds to a struct timeval for e.g. select. |
161 | | */ |
162 | | struct timeval MillisToTimeval(int64_t nTimeout); |
163 | | |
164 | | /** |
165 | | * Convert milliseconds to a struct timeval for e.g. select. |
166 | | */ |
167 | | struct timeval MillisToTimeval(std::chrono::milliseconds ms); |
168 | | |
169 | | #endif // BITCOIN_UTIL_TIME_H |