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 <stdint.h> 10 : #include <string> 11 : #include <chrono> 12 : 13 : void UninterruptibleSleep(const std::chrono::microseconds& n); 14 : 15 : /** 16 : * Helper to count the seconds of a duration. 17 : * 18 : * All durations should be using std::chrono and calling this should generally 19 : * be avoided in code. Though, it is still preferred to an inline t.count() to 20 : * protect against a reliance on the exact type of t. 21 : * 22 : * This helper is used to convert durations before passing them over an 23 : * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) 24 : */ 25 72252 : inline int64_t count_seconds(std::chrono::seconds t) { return t.count(); } 26 10118 : inline int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } 27 : 28 : /** 29 : * DEPRECATED 30 : * Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable) 31 : */ 32 : int64_t GetTime(); 33 : 34 : /** Returns the system time (not mockable) */ 35 : int64_t GetTimeMillis(); 36 : /** Returns the system time (not mockable) */ 37 : int64_t GetTimeMicros(); 38 : /** Returns the system time (not mockable) */ 39 : int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable 40 : 41 : /** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ 42 : void SetMockTime(int64_t nMockTimeIn); 43 : /** For testing */ 44 : int64_t GetMockTime(); 45 : 46 : /** Return system time (or mocked time, if set) */ 47 : template <typename T> 48 : T GetTime(); 49 : 50 : /** 51 : * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} 52 : * helper functions if possible. 53 : */ 54 : std::string FormatISO8601DateTime(int64_t nTime); 55 : std::string FormatISO8601Date(int64_t nTime); 56 : int64_t ParseISO8601DateTime(const std::string& str); 57 : 58 : #endif // BITCOIN_UTIL_TIME_H