/Users/eugenesiegel/btc/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 |  |     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 | 681k | { | 
| 75 | 681k |     return std::chrono::duration_cast<Dur1>(d).count(); | 
| 76 | 681k | } _Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEES5_EDaT0_| Line | Count | Source |  | 74 | 102k | { |  | 75 | 102k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 102k | } | 
Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000000EEEEENS2_IxNS3_ILl1ELl1000000000EEEEEEDaT0__Z5TicksINSt3__16chrono8durationIdNS0_5ratioILl1ELl1000EEEEENS2_IxNS3_ILl1ELl1000000000EEEEEEDaT0_| Line | Count | Source |  | 74 | 13.7k | { |  | 75 | 13.7k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 13.7k | } | 
Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIdNS0_5ratioILl1ELl1EEEEENS2_IxNS3_ILl1ELl1000000000EEEEEEDaT0__Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000000EEEEES5_EDaT0_| Line | Count | Source |  | 74 | 102k | { |  | 75 | 102k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 102k | } | 
Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS2_IxNS3_ILl1ELl1000000000EEEEEEDaT0_Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000EEEEENS2_IxNS3_ILl1ELl1000000000EEEEEEDaT0__Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS2_IxNS3_ILl1ELl1000000EEEEEEDaT0_| Line | Count | Source |  | 74 | 213k | { |  | 75 | 213k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 213k | } | 
_Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS2_IlNS3_ILl3600ELl1EEEEEEDaT0_| Line | Count | Source |  | 74 | 197k | { |  | 75 | 197k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 197k | } | 
_Z5TicksINSt3__16chrono8durationIlNS0_5ratioILl3600ELl1EEEEES5_EDaT0_| Line | Count | Source |  | 74 | 51.2k | { |  | 75 | 51.2k |     return std::chrono::duration_cast<Dur1>(d).count(); |  | 76 | 51.2k | } | 
Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIdNS0_5ratioILl3600ELl1EEEEENS2_IxNS3_ILl1ELl1EEEEEEDaT0_Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIdNS0_5ratioILl1ELl1EEEEENS2_IxNS3_ILl1ELl1000000EEEEEEDaT0_Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIlNS0_5ratioILl60ELl1EEEEES5_EDaT0_Unexecuted instantiation: _Z5TicksINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000EEEEENS2_IxNS3_ILl1ELl1000000EEEEEEDaT0_ | 
| 77 |  | template <typename Duration, typename Timepoint> | 
| 78 |  | constexpr auto TicksSinceEpoch(Timepoint t) | 
| 79 | 248k | { | 
| 80 | 248k |     return Ticks<Duration>(t.time_since_epoch()); | 
| 81 | 248k | } Unexecuted instantiation: _Z15TicksSinceEpochINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS1_10time_pointI9NodeClockS5_EEEDaT0__Z15TicksSinceEpochINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS1_10time_pointINS1_12system_clockES5_EEEDaT0_| Line | Count | Source |  | 79 | 102k | { |  | 80 | 102k |     return Ticks<Duration>(t.time_since_epoch()); |  | 81 | 102k | } | 
_Z15TicksSinceEpochINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEENS1_10time_pointI9NodeClockNS2_IxNS3_ILl1ELl1000000EEEEEEEEDaT0_| Line | Count | Source |  | 79 | 146k | { |  | 80 | 146k |     return Ticks<Duration>(t.time_since_epoch()); |  | 81 | 146k | } | 
Unexecuted instantiation: _Z15TicksSinceEpochINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000EEEEENS1_10time_pointINS1_12system_clockENS2_IxNS3_ILl1ELl1000000EEEEEEEEDaT0_ | 
| 82 | 683k | constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } | 
| 83 | 0 | constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } | 
| 84 | 0 | constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } | 
| 85 |  |  | 
| 86 |  | using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; | 
| 87 |  | using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; | 
| 88 |  | using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; | 
| 89 |  |  | 
| 90 |  | /** | 
| 91 |  |  * DEPRECATED | 
| 92 |  |  * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. | 
| 93 |  |  * ClockType is | 
| 94 |  |  * - SteadyClock/std::chrono::steady_clock for steady time | 
| 95 |  |  * - SystemClock/std::chrono::system_clock for system time | 
| 96 |  |  * - NodeClock                             for mockable system time | 
| 97 |  |  */ | 
| 98 |  | int64_t GetTime(); | 
| 99 |  |  | 
| 100 |  | /** | 
| 101 |  |  * DEPRECATED | 
| 102 |  |  * Use SetMockTime with chrono type | 
| 103 |  |  * | 
| 104 |  |  * @param[in] nMockTimeIn Time in seconds. | 
| 105 |  |  */ | 
| 106 |  | void SetMockTime(int64_t nMockTimeIn); | 
| 107 |  |  | 
| 108 |  | /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ | 
| 109 |  | void SetMockTime(std::chrono::seconds mock_time_in); | 
| 110 |  | void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock); | 
| 111 |  |  | 
| 112 |  | /** For testing */ | 
| 113 |  | std::chrono::seconds GetMockTime(); | 
| 114 |  |  | 
| 115 |  | /** | 
| 116 |  |  * Return the current time point cast to the given precision. Only use this | 
| 117 |  |  * when an exact precision is needed, otherwise use T::clock::now() directly. | 
| 118 |  |  */ | 
| 119 |  | template <typename T> | 
| 120 |  | T Now() | 
| 121 | 32.7M | { | 
| 122 | 32.7M |     return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); | 
| 123 | 32.7M | } _Z3NowINSt3__16chrono10time_pointI9NodeClockNS1_8durationIxNS0_5ratioILl1ELl1EEEEEEEET_v| Line | Count | Source |  | 121 | 13.1M | { |  | 122 | 13.1M |     return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |  | 123 | 13.1M | } | 
_Z3NowINSt3__16chrono10time_pointINS1_12steady_clockENS1_8durationIxNS0_5ratioILl1ELl1EEEEEEEET_v| Line | Count | Source |  | 121 | 2 | { |  | 122 | 2 |     return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |  | 123 | 2 | } | 
Unexecuted instantiation: _Z3NowINSt3__16chrono10time_pointINS1_12steady_clockENS1_8durationIxNS0_5ratioILl1ELl1000EEEEEEEET_vUnexecuted instantiation: _Z3NowINSt3__16chrono10time_pointI9NodeClockNS1_8durationIxNS0_5ratioILl1ELl1000EEEEEEEET_v_Z3NowINSt3__16chrono10time_pointI9NodeClockNS1_8durationIxNS0_5ratioILl1ELl1000000EEEEEEEET_v| Line | Count | Source |  | 121 | 19.6M | { |  | 122 | 19.6M |     return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); |  | 123 | 19.6M | } | 
 | 
| 124 |  | /** DEPRECATED, see GetTime */ | 
| 125 |  | template <typename T> | 
| 126 |  | T GetTime() | 
| 127 | 31.5M | { | 
| 128 | 31.5M |     return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); | 
| 129 | 31.5M | } Unexecuted instantiation: _Z7GetTimeINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000EEEEEET_v_Z7GetTimeINSt3__16chrono8durationIxNS0_5ratioILl1ELl1EEEEEET_v| Line | Count | Source |  | 127 | 11.9M | { |  | 128 | 11.9M |     return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); |  | 129 | 11.9M | } | 
_Z7GetTimeINSt3__16chrono8durationIxNS0_5ratioILl1ELl1000000EEEEEET_v| Line | Count | Source |  | 127 | 19.6M | { |  | 128 | 19.6M |     return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); |  | 129 | 19.6M | } | 
 | 
| 130 |  |  | 
| 131 |  | /** | 
| 132 |  |  * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} | 
| 133 |  |  * helper functions if possible. | 
| 134 |  |  */ | 
| 135 |  | std::string FormatISO8601DateTime(int64_t nTime); | 
| 136 |  | std::string FormatISO8601Date(int64_t nTime); | 
| 137 |  | std::optional<int64_t> ParseISO8601DateTime(std::string_view str); | 
| 138 |  |  | 
| 139 |  | /** | 
| 140 |  |  * Convert milliseconds to a struct timeval for e.g. select. | 
| 141 |  |  */ | 
| 142 |  | struct timeval MillisToTimeval(int64_t nTimeout); | 
| 143 |  |  | 
| 144 |  | /** | 
| 145 |  |  * Convert milliseconds to a struct timeval for e.g. select. | 
| 146 |  |  */ | 
| 147 |  | struct timeval MillisToTimeval(std::chrono::milliseconds ms); | 
| 148 |  |  | 
| 149 |  | #endif // BITCOIN_UTIL_TIME_H |