Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto # 2 : : // Copyright (c) 2009-2020 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 <compat.h> # 10 : : # 11 : : #include <chrono> # 12 : : #include <stdint.h> # 13 : : #include <string> # 14 : : # 15 : : using namespace std::chrono_literals; # 16 : : # 17 : : void UninterruptibleSleep(const std::chrono::microseconds& n); # 18 : : # 19 : : /** # 20 : : * Helper to count the seconds of a duration. # 21 : : * # 22 : : * All durations should be using std::chrono and calling this should generally # 23 : : * be avoided in code. Though, it is still preferred to an inline t.count() to # 24 : : * protect against a reliance on the exact type of t. # 25 : : * # 26 : : * This helper is used to convert durations before passing them over an # 27 : : * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) # 28 : : */ # 29 : 276628 : constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } # 30 : 562 : constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } # 31 : 1 : constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } # 32 : : # 33 : : using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>; # 34 : : # 35 : : /** # 36 : : * Helper to count the seconds in any std::chrono::duration type # 37 : : */ # 38 : 18537 : inline double CountSecondsDouble(SecondsDouble t) { return t.count(); } # 39 : : # 40 : : /** # 41 : : * DEPRECATED # 42 : : * Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable) # 43 : : */ # 44 : : int64_t GetTime(); # 45 : : # 46 : : /** Returns the system time (not mockable) */ # 47 : : int64_t GetTimeMillis(); # 48 : : /** Returns the system time (not mockable) */ # 49 : : int64_t GetTimeMicros(); # 50 : : /** Returns the system time (not mockable) */ # 51 : : int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable # 52 : : # 53 : : /** # 54 : : * DEPRECATED # 55 : : * Use SetMockTime with chrono type # 56 : : * # 57 : : * @param[in] nMockTimeIn Time in seconds. # 58 : : */ # 59 : : void SetMockTime(int64_t nMockTimeIn); # 60 : : # 61 : : /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ # 62 : : void SetMockTime(std::chrono::seconds mock_time_in); # 63 : : # 64 : : /** For testing */ # 65 : : std::chrono::seconds GetMockTime(); # 66 : : # 67 : : /** Return system time (or mocked time, if set) */ # 68 : : template <typename T> # 69 : : T GetTime(); # 70 : : # 71 : : /** # 72 : : * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} # 73 : : * helper functions if possible. # 74 : : */ # 75 : : std::string FormatISO8601DateTime(int64_t nTime); # 76 : : std::string FormatISO8601Date(int64_t nTime); # 77 : : int64_t ParseISO8601DateTime(const std::string& str); # 78 : : # 79 : : /** # 80 : : * Convert milliseconds to a struct timeval for e.g. select. # 81 : : */ # 82 : : struct timeval MillisToTimeval(int64_t nTimeout); # 83 : : # 84 : : /** # 85 : : * Convert milliseconds to a struct timeval for e.g. select. # 86 : : */ # 87 : : struct timeval MillisToTimeval(std::chrono::milliseconds ms); # 88 : : # 89 : : /** Sanity check epoch match normal Unix epoch */ # 90 : : bool ChronoSanityCheck(); # 91 : : # 92 : : #endif // BITCOIN_UTIL_TIME_H