/root/bitcoin/src/util/time.h
Line | Count | Source (jump to first uncovered line) |
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 | | /** 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 | | |
27 | | using SteadyClock = std::chrono::steady_clock; |
28 | | using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; |
29 | | using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; |
30 | | using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; |
31 | | |
32 | | using SystemClock = std::chrono::system_clock; |
33 | | |
34 | | /** |
35 | | * Version of SteadyClock that is mockable in the context of tests (set the |
36 | | * current value with SetMockTime), otherwise the system steady clock. |
37 | | */ |
38 | | struct MockableSteadyClock : public std::chrono::steady_clock { |
39 | | using time_point = std::chrono::time_point<MockableSteadyClock>; |
40 | | |
41 | | static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1}; |
42 | | |
43 | | /** Return current system time or mocked time, if set */ |
44 | | static time_point now() noexcept; |
45 | | static std::time_t to_time_t(const time_point&) = delete; // unused |
46 | | static time_point from_time_t(std::time_t) = delete; // unused |
47 | | |
48 | | /** Set mock time for testing. |
49 | | * When mocking the steady clock, start at INITIAL_MOCK_TIME and add durations to elapse time as necessary |
50 | | * for testing. |
51 | | * To stop mocking, call ClearMockTime(). |
52 | | */ |
53 | | static void SetMockTime(std::chrono::milliseconds mock_time_in); |
54 | | |
55 | | /** Clear mock time, go back to system steady clock. */ |
56 | | static void ClearMockTime(); |
57 | | }; |
58 | | |
59 | | void UninterruptibleSleep(const std::chrono::microseconds& n); |
60 | | |
61 | | /** |
62 | | * Helper to count the seconds of a duration/time_point. |
63 | | * |
64 | | * All durations/time_points should be using std::chrono and calling this should generally |
65 | | * be avoided in code. Though, it is still preferred to an inline t.count() to |
66 | | * protect against a reliance on the exact type of t. |
67 | | * |
68 | | * This helper is used to convert durations/time_points before passing them over an |
69 | | * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) |
70 | | */ |
71 | | template <typename Dur1, typename Dur2> |
72 | | constexpr auto Ticks(Dur2 d) |
73 | 0 | { |
74 | 0 | return std::chrono::duration_cast<Dur1>(d).count(); |
75 | 0 | } Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEES4_EDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1000000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1000EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS1_IlS2_ILl3600ELl1EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl3600ELl1EEEES4_EDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl3600ELl1EEEENS1_IlS2_ILl1ELl1EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIdSt5ratioILl1ELl1EEEENS1_IlS2_ILl1ELl1000000EEEEEDaT0_ Unexecuted instantiation: _Z5TicksINSt6chrono8durationIlSt5ratioILl60ELl1EEEES4_EDaT0_ |
76 | | template <typename Duration, typename Timepoint> |
77 | | constexpr auto TicksSinceEpoch(Timepoint t) |
78 | 0 | { |
79 | 0 | return Ticks<Duration>(t.time_since_epoch()); |
80 | 0 | } Unexecuted instantiation: _Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointI9NodeClockS4_EEEDaT0_ Unexecuted instantiation: _Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointINS0_3_V212system_clockES4_EEEDaT0_ Unexecuted instantiation: _Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1EEEENS0_10time_pointI9NodeClockNS1_IlS2_ILl1ELl1000000000EEEEEEEDaT0_ Unexecuted instantiation: _Z15TicksSinceEpochINSt6chrono8durationIlSt5ratioILl1ELl1000EEEENS0_10time_pointINS0_3_V212system_clockENS1_IlS2_ILl1ELl1000000000EEEEEEEDaT0_ |
81 | 0 | constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } |
82 | 0 | constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } |
83 | 0 | constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } |
84 | | |
85 | | using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; |
86 | | using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; |
87 | | using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; |
88 | | |
89 | | /** |
90 | | * DEPRECATED |
91 | | * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. |
92 | | * ClockType is |
93 | | * - SteadyClock/std::chrono::steady_clock for steady time |
94 | | * - SystemClock/std::chrono::system_clock for system time |
95 | | * - NodeClock for mockable system time |
96 | | */ |
97 | | int64_t GetTime(); |
98 | | |
99 | | /** |
100 | | * DEPRECATED |
101 | | * Use SetMockTime with chrono type |
102 | | * |
103 | | * @param[in] nMockTimeIn Time in seconds. |
104 | | */ |
105 | | void SetMockTime(int64_t nMockTimeIn); |
106 | | |
107 | | /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ |
108 | | void SetMockTime(std::chrono::seconds mock_time_in); |
109 | | |
110 | | /** For testing */ |
111 | | std::chrono::seconds GetMockTime(); |
112 | | |
113 | | /** |
114 | | * Return the current time point cast to the given precision. Only use this |
115 | | * when an exact precision is needed, otherwise use T::clock::now() directly. |
116 | | */ |
117 | | template <typename T> |
118 | | T Now() |
119 | 0 | { |
120 | 0 | return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |
121 | 0 | } Unexecuted instantiation: _Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1EEEEEEET_v Unexecuted instantiation: _Z3NowINSt6chrono10time_pointINS0_3_V212steady_clockENS0_8durationIlSt5ratioILl1ELl1000EEEEEEET_v Unexecuted instantiation: _Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1000EEEEEEET_v Unexecuted instantiation: _Z3NowINSt6chrono10time_pointI9NodeClockNS0_8durationIlSt5ratioILl1ELl1000000EEEEEEET_v |
122 | | /** DEPRECATED, see GetTime */ |
123 | | template <typename T> |
124 | | T GetTime() |
125 | 0 | { |
126 | 0 | return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); |
127 | 0 | } Unexecuted instantiation: _Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1000EEEEET_v Unexecuted instantiation: _Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1EEEEET_v Unexecuted instantiation: _Z7GetTimeINSt6chrono8durationIlSt5ratioILl1ELl1000000EEEEET_v |
128 | | |
129 | | /** |
130 | | * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} |
131 | | * helper functions if possible. |
132 | | */ |
133 | | std::string FormatISO8601DateTime(int64_t nTime); |
134 | | std::string FormatISO8601Date(int64_t nTime); |
135 | | std::optional<int64_t> ParseISO8601DateTime(std::string_view str); |
136 | | |
137 | | /** |
138 | | * Convert milliseconds to a struct timeval for e.g. select. |
139 | | */ |
140 | | struct timeval MillisToTimeval(int64_t nTimeout); |
141 | | |
142 | | /** |
143 | | * Convert milliseconds to a struct timeval for e.g. select. |
144 | | */ |
145 | | struct timeval MillisToTimeval(std::chrono::milliseconds ms); |
146 | | |
147 | | #endif // BITCOIN_UTIL_TIME_H |