Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : : // Copyright (c) 2009-2022 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 <string> 12 : : 13 : : using namespace std::chrono_literals; 14 : : 15 : : /** Mockable clock in the context of tests, otherwise the system clock */ 16 : : struct NodeClock : public std::chrono::system_clock { 17 : : using time_point = std::chrono::time_point<NodeClock>; 18 : : /** Return current system time or mocked time, if set */ 19 : : static time_point now() noexcept; 20 : : static std::time_t to_time_t(const time_point&) = delete; // unused 21 : : static time_point from_time_t(std::time_t) = delete; // unused 22 : : }; 23 : : using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>; 24 : : 25 : : using SteadyClock = std::chrono::steady_clock; 26 : : using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>; 27 : : using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>; 28 : : using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>; 29 : : 30 : : using SystemClock = std::chrono::system_clock; 31 : : 32 : : void UninterruptibleSleep(const std::chrono::microseconds& n); 33 : : 34 : : /** 35 : : * Helper to count the seconds of a duration/time_point. 36 : : * 37 : : * All durations/time_points should be using std::chrono and calling this should generally 38 : : * be avoided in code. Though, it is still preferred to an inline t.count() to 39 : : * protect against a reliance on the exact type of t. 40 : : * 41 : : * This helper is used to convert durations/time_points before passing them over an 42 : : * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) 43 : : */ 44 : : template <typename Dur1, typename Dur2> 45 : 412 : constexpr auto Ticks(Dur2 d) 46 : : { 47 : 412 : return std::chrono::duration_cast<Dur1>(d).count(); 48 : : } 49 : : template <typename Duration, typename Timepoint> 50 : 402 : constexpr auto TicksSinceEpoch(Timepoint t) 51 : : { 52 : 402 : return Ticks<Duration>(t.time_since_epoch()); 53 : : } 54 : 289 : constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } 55 : 0 : constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } 56 : 0 : constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } 57 : : 58 : : using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>; 59 : : using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; 60 : : using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>; 61 : : 62 : : /** 63 : : * DEPRECATED 64 : : * Use either ClockType::now() or Now<TimePointType>() if a cast is needed. 65 : : * ClockType is 66 : : * - SteadyClock/std::chrono::steady_clock for steady time 67 : : * - SystemClock/std::chrono::system_clock for system time 68 : : * - NodeClock for mockable system time 69 : : */ 70 : : int64_t GetTime(); 71 : : 72 : : /** 73 : : * DEPRECATED 74 : : * Use SetMockTime with chrono type 75 : : * 76 : : * @param[in] nMockTimeIn Time in seconds. 77 : : */ 78 : : void SetMockTime(int64_t nMockTimeIn); 79 : : 80 : : /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ 81 : : void SetMockTime(std::chrono::seconds mock_time_in); 82 : : 83 : : /** For testing */ 84 : : std::chrono::seconds GetMockTime(); 85 : : 86 : : /** 87 : : * Return the current time point cast to the given precision. Only use this 88 : : * when an exact precision is needed, otherwise use T::clock::now() directly. 89 : : */ 90 : : template <typename T> 91 : 3805 : T Now() 92 : : { 93 : 3805 : return std::chrono::time_point_cast<typename T::duration>(T::clock::now()); 94 : : } 95 : : /** DEPRECATED, see GetTime */ 96 : : template <typename T> 97 : 2084 : T GetTime() 98 : : { 99 : 2084 : return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch(); 100 : : } 101 : : 102 : : /** 103 : : * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} 104 : : * helper functions if possible. 105 : : */ 106 : : std::string FormatISO8601DateTime(int64_t nTime); 107 : : std::string FormatISO8601Date(int64_t nTime); 108 : : 109 : : /** 110 : : * Convert milliseconds to a struct timeval for e.g. select. 111 : : */ 112 : : struct timeval MillisToTimeval(int64_t nTimeout); 113 : : 114 : : /** 115 : : * Convert milliseconds to a struct timeval for e.g. select. 116 : : */ 117 : : struct timeval MillisToTimeval(std::chrono::milliseconds ms); 118 : : 119 : : /** Sanity check epoch match normal Unix epoch */ 120 : : bool ChronoSanityCheck(); 121 : : 122 : : #endif // BITCOIN_UTIL_TIME_H