Branch data Line data Source code
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<std::chrono::milliseconds> g_mock_steady_time{}; //!< For testing
25 : :
26 : 24098321 : NodeClock::time_point NodeClock::now() noexcept
27 : : {
28 [ + + ]: 24098321 : const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
29 [ + + ]: 24098321 : if (!mocktime.count()) {
30 : 16795 : g_used_system_time = true;
31 : : }
32 [ + + ]: 24098321 : const auto ret{
33 [ + + ]: 24098321 : mocktime.count() ?
34 : : mocktime :
35 : 24098321 : std::chrono::system_clock::now().time_since_epoch()};
36 [ - + ]: 24098321 : assert(ret > 0s);
37 : 24098321 : return time_point{ret};
38 : : };
39 : :
40 : 538151 : void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
41 : 735447 : void SetMockTime(std::chrono::seconds mock_time_in)
42 : : {
43 : 735447 : Assert(mock_time_in >= 0s);
44 : 735447 : g_mock_time.store(mock_time_in, std::memory_order_relaxed);
45 : 735447 : }
46 : :
47 : 6361586 : std::chrono::seconds GetMockTime()
48 : : {
49 : 6361586 : return g_mock_time.load(std::memory_order_relaxed);
50 : : }
51 : :
52 : 0 : MockableSteadyClock::time_point MockableSteadyClock::now() noexcept
53 : : {
54 [ # # ]: 0 : const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)};
55 [ # # ]: 0 : if (!mocktime.count()) {
56 : 0 : g_used_system_time = true;
57 : : }
58 [ # # ]: 0 : const auto ret{
59 [ # # ]: 0 : mocktime.count() ?
60 : : mocktime :
61 : 0 : std::chrono::steady_clock::now().time_since_epoch()};
62 : 0 : return time_point{ret};
63 : : };
64 : :
65 : 93121 : void MockableSteadyClock::SetMockTime(std::chrono::milliseconds mock_time_in)
66 : : {
67 : 93121 : Assert(mock_time_in >= 0s);
68 : 93121 : g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed);
69 : 93121 : }
70 : :
71 : 0 : void MockableSteadyClock::ClearMockTime()
72 : : {
73 : 0 : g_mock_steady_time.store(0ms, std::memory_order_relaxed);
74 : 0 : }
75 : :
76 : 2241924 : int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
77 : :
78 : 12883978 : std::string FormatISO8601DateTime(int64_t nTime)
79 : : {
80 : 12883978 : const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
81 : 12883978 : const auto days{std::chrono::floor<std::chrono::days>(secs)};
82 : 12883978 : const std::chrono::year_month_day ymd{days};
83 : 12883978 : const std::chrono::hh_mm_ss hms{secs - days};
84 : 12883978 : 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());
85 : : }
86 : :
87 : 5244 : std::string FormatISO8601Date(int64_t nTime)
88 : : {
89 : 5244 : const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
90 : 5244 : const auto days{std::chrono::floor<std::chrono::days>(secs)};
91 : 5244 : const std::chrono::year_month_day ymd{days};
92 : 5244 : return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
93 : : }
94 : :
95 : 342 : std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
96 : : {
97 : 342 : constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
98 [ + + + + : 342 : if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
+ + + + +
+ + + +
+ ]
99 : 126 : return {};
100 : : }
101 : 216 : const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
102 : 216 : const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
103 : 216 : const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
104 : 216 : const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
105 : 216 : const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
106 : 216 : const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
107 [ + + + + : 216 : if (!year || !month || !day || !hour || !min || !sec) {
+ + + + +
+ + + ]
108 : 6 : return {};
109 : : }
110 [ + + ]: 210 : const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
111 [ + + ]: 210 : if (!ymd.ok()) {
112 : 4 : return {};
113 : : }
114 : 206 : const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
115 : 206 : const auto tp{std::chrono::sys_days{ymd} + time};
116 : 206 : return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
117 : : }
118 : :
119 : 470 : struct timeval MillisToTimeval(int64_t nTimeout)
120 : : {
121 : 470 : struct timeval timeout;
122 : 470 : timeout.tv_sec = nTimeout / 1000;
123 : 470 : timeout.tv_usec = (nTimeout % 1000) * 1000;
124 : 470 : return timeout;
125 : : }
126 : :
127 : 0 : struct timeval MillisToTimeval(std::chrono::milliseconds ms)
128 : : {
129 : 0 : return MillisToTimeval(count_milliseconds(ms));
130 : : }
|