/Users/eugenesiegel/btc/bitcoin/src/util/time.cpp
| 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 |  | #include <util/time.h> | 
| 7 |  |  | 
| 8 |  | #include <compat/compat.h> | 
| 9 |  | #include <tinyformat.h> | 
| 10 |  | #include <util/check.h> | 
| 11 |  | #include <util/strencodings.h> | 
| 12 |  |  | 
| 13 |  | #include <atomic> | 
| 14 |  | #include <chrono> | 
| 15 |  | #include <optional> | 
| 16 |  | #include <string> | 
| 17 |  | #include <string_view> | 
| 18 |  | #include <thread> | 
| 19 |  |  | 
| 20 | 0 | void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } | 
| 21 |  |  | 
| 22 |  | static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing | 
| 23 |  | std::atomic<bool> g_used_system_time{false}; | 
| 24 |  | static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing | 
| 25 |  |  | 
| 26 |  | NodeClock::time_point NodeClock::now() noexcept | 
| 27 | 35.6M | { | 
| 28 | 35.6M |     const auto mocktime{g_mock_time.load(std::memory_order_relaxed)}; | 
| 29 | 35.6M |     if (!mocktime.count()) { | 
| 30 | 0 |         g_used_system_time = true; | 
| 31 | 0 |     } | 
| 32 | 35.6M |     const auto ret{ | 
| 33 | 35.6M |         mocktime.count() ? | 
| 34 | 35.6M |             mocktime : | 
| 35 | 35.6M |             std::chrono::system_clock::now().time_since_epoch()0}; | 
| 36 | 35.6M |     assert(ret > 0s); | 
| 37 | 35.6M |     return time_point{ret}; | 
| 38 | 35.6M | }; | 
| 39 |  |  | 
| 40 | 67.5k | void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); } | 
| 41 | 0 | void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); } | 
| 42 |  | void SetMockTime(std::chrono::seconds mock_time_in) | 
| 43 | 170k | { | 
| 44 | 170k |     Assert(mock_time_in >= 0s); | Line | Count | Source |  | 106 | 170k | #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val) | 
 | 
| 45 | 170k |     g_mock_time.store(mock_time_in, std::memory_order_relaxed); | 
| 46 | 170k | } | 
| 47 |  |  | 
| 48 |  | std::chrono::seconds GetMockTime() | 
| 49 | 102k | { | 
| 50 | 102k |     return g_mock_time.load(std::memory_order_relaxed); | 
| 51 | 102k | } | 
| 52 |  |  | 
| 53 |  | MockableSteadyClock::time_point MockableSteadyClock::now() noexcept | 
| 54 | 0 | { | 
| 55 | 0 |     const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)}; | 
| 56 | 0 |     if (!mocktime.count()) { | 
| 57 | 0 |         g_used_system_time = true; | 
| 58 | 0 |     } | 
| 59 | 0 |     const auto ret{ | 
| 60 | 0 |         mocktime.count() ? | 
| 61 | 0 |             mocktime : | 
| 62 | 0 |             std::chrono::steady_clock::now().time_since_epoch()}; | 
| 63 | 0 |     return time_point{ret}; | 
| 64 | 0 | }; | 
| 65 |  |  | 
| 66 |  | void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in) | 
| 67 | 192k | { | 
| 68 | 192k |     Assert(mock_time_in >= 0s); | Line | Count | Source |  | 106 | 192k | #define Assert(val) inline_assertion_check<true>(val, __FILE__, __LINE__, __func__, #val) | 
 | 
| 69 | 192k |     g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed); | 
| 70 | 192k | } | 
| 71 |  |  | 
| 72 |  | void MockableSteadyClock::ClearMockTime() | 
| 73 | 0 | { | 
| 74 | 0 |     g_mock_steady_time.store(0ms, std::memory_order_relaxed); | 
| 75 | 0 | } | 
| 76 |  |  | 
| 77 | 1.09M | int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); } | 
| 78 |  |  | 
| 79 |  | std::string FormatISO8601DateTime(int64_t nTime) | 
| 80 | 190k | { | 
| 81 | 190k |     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; | 
| 82 | 190k |     const auto days{std::chrono::floor<std::chrono::days>(secs)}; | 
| 83 | 190k |     const std::chrono::year_month_day ymd{days}; | 
| 84 | 190k |     const std::chrono::hh_mm_ss hms{secs - days}; | 
| 85 | 190k |     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());| Line | Count | Source |  | 1172 | 190k | #define strprintf tfm::format | 
 | 
| 86 | 190k | } | 
| 87 |  |  | 
| 88 |  | std::string FormatISO8601Date(int64_t nTime) | 
| 89 | 102k | { | 
| 90 | 102k |     const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; | 
| 91 | 102k |     const auto days{std::chrono::floor<std::chrono::days>(secs)}; | 
| 92 | 102k |     const std::chrono::year_month_day ymd{days}; | 
| 93 | 102k |     return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});| Line | Count | Source |  | 1172 | 102k | #define strprintf tfm::format | 
 | 
| 94 | 102k | } | 
| 95 |  |  | 
| 96 |  | std::optional<int64_t> ParseISO8601DateTime(std::string_view str) | 
| 97 | 2 | { | 
| 98 | 2 |     constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()}; | 
| 99 | 2 |     if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') { | 
| 100 | 0 |         return {}; | 
| 101 | 0 |     } | 
| 102 | 2 |     const auto year{ToIntegral<uint16_t>(str.substr(0, 4))}; | 
| 103 | 2 |     const auto month{ToIntegral<uint8_t>(str.substr(5, 2))}; | 
| 104 | 2 |     const auto day{ToIntegral<uint8_t>(str.substr(8, 2))}; | 
| 105 | 2 |     const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))}; | 
| 106 | 2 |     const auto min{ToIntegral<uint8_t>(str.substr(14, 2))}; | 
| 107 | 2 |     const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))}; | 
| 108 | 2 |     if (!year || !month || !day || !hour || !min || !sec) { | 
| 109 | 0 |         return {}; | 
| 110 | 0 |     } | 
| 111 | 2 |     const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}}; | 
| 112 | 2 |     if (!ymd.ok()) { | 
| 113 | 0 |         return {}; | 
| 114 | 0 |     } | 
| 115 | 2 |     const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}}; | 
| 116 | 2 |     const auto tp{std::chrono::sys_days{ymd} + time}; | 
| 117 | 2 |     return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)}; | 
| 118 | 2 | } | 
| 119 |  |  | 
| 120 |  | struct timeval MillisToTimeval(int64_t nTimeout) | 
| 121 | 0 | { | 
| 122 | 0 |     struct timeval timeout; | 
| 123 | 0 |     timeout.tv_sec  = nTimeout / 1000; | 
| 124 | 0 |     timeout.tv_usec = (nTimeout % 1000) * 1000; | 
| 125 | 0 |     return timeout; | 
| 126 | 0 | } | 
| 127 |  |  | 
| 128 |  | struct timeval MillisToTimeval(std::chrono::milliseconds ms) | 
| 129 | 0 | { | 
| 130 | 0 |     return MillisToTimeval(count_milliseconds(ms)); | 
| 131 | 0 | } |