Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2021 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 : #if defined(HAVE_CONFIG_H) 7 : #include <config/bitcoin-config.h> 8 : #endif 9 : 10 : #include <compat/compat.h> 11 : #include <tinyformat.h> 12 : #include <util/time.h> 13 : #include <util/check.h> 14 : 15 : #include <atomic> 16 : #include <chrono> 17 : #include <ctime> 18 : #include <locale> 19 : #include <thread> 20 : #include <sstream> 21 : #include <string> 22 : 23 304 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } 24 : 25 : static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing 26 : 27 435914 : NodeClock::time_point NodeClock::now() noexcept 28 : { 29 435914 : const auto mocktime{g_mock_time.load(std::memory_order_relaxed)}; 30 : const auto ret{ 31 871828 : mocktime.count() ? 32 387423 : mocktime : 33 48491 : std::chrono::system_clock::now().time_since_epoch()}; 34 435914 : assert(ret > 0s); 35 435914 : return time_point{ret}; 36 : }; 37 : 38 : template <typename T> 39 3611278 : static T GetSystemTime() 40 : { 41 3611278 : const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch()); 42 3611278 : assert(now.count() > 0); 43 3611278 : return now; 44 : } 45 : 46 11977 : void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); } 47 12614 : void SetMockTime(std::chrono::seconds mock_time_in) 48 : { 49 12614 : Assert(mock_time_in >= 0s); 50 12614 : g_mock_time.store(mock_time_in, std::memory_order_relaxed); 51 12614 : } 52 : 53 2146135 : std::chrono::seconds GetMockTime() 54 : { 55 2146135 : return g_mock_time.load(std::memory_order_relaxed); 56 : } 57 : 58 3611278 : int64_t GetTimeMicros() 59 : { 60 3611278 : return int64_t{GetSystemTime<std::chrono::microseconds>().count()}; 61 : } 62 : 63 403423 : int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); } 64 : 65 2171239 : std::string FormatISO8601DateTime(int64_t nTime) 66 : { 67 2171239 : const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; 68 2171239 : const auto days{std::chrono::floor<std::chrono::days>(secs)}; 69 2171239 : const std::chrono::year_month_day ymd{days}; 70 2171239 : const std::chrono::hh_mm_ss hms{secs - days}; 71 2171239 : return strprintf("%04i-%02u-%02uT%02i:%02i:%02iZ", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count()); 72 : } 73 : 74 372 : std::string FormatISO8601Date(int64_t nTime) 75 : { 76 372 : const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; 77 372 : const auto days{std::chrono::floor<std::chrono::days>(secs)}; 78 372 : const std::chrono::year_month_day ymd{days}; 79 372 : return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}); 80 : } 81 : 82 2 : std::string FormatISO8601Time(int64_t nTime) 83 : { 84 2 : const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; 85 2 : const auto days{std::chrono::floor<std::chrono::days>(secs)}; 86 2 : const std::chrono::hh_mm_ss hms{secs - days}; 87 2 : return strprintf("%02i:%02i:%02iZ", hms.hours().count(), hms.minutes().count(), hms.seconds().count()); 88 : } 89 : 90 0 : struct timespec MillisToTimespec(int64_t nTimeout) 91 : { 92 : struct timespec timeout; 93 0 : timeout.tv_sec = nTimeout / 1000; 94 0 : timeout.tv_nsec = (nTimeout % 1000) * 1000 * 1000; 95 0 : return timeout; 96 : } 97 : 98 2 : struct timeval MillisToTimeval(int64_t nTimeout) 99 : { 100 : struct timeval timeout; 101 2 : timeout.tv_sec = nTimeout / 1000; 102 2 : timeout.tv_usec = (nTimeout % 1000) * 1000; 103 2 : return timeout; 104 : } 105 : 106 0 : struct timespec MillisToTimespec(std::chrono::milliseconds ms) 107 : { 108 0 : return MillisToTimespec(count_milliseconds(ms)); 109 : } 110 : 111 2 : struct timeval MillisToTimeval(std::chrono::milliseconds ms) 112 : { 113 2 : return MillisToTimeval(count_milliseconds(ms)); 114 : }