/root/bitcoin/src/util/time.cpp
Line | Count | Source |
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 <array> |
14 | | #include <atomic> |
15 | | #include <chrono> |
16 | | #include <optional> |
17 | | #include <string> |
18 | | #include <string_view> |
19 | | #include <thread> |
20 | | |
21 | | static constexpr std::array<std::string_view, 7> weekdays{"Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed"}; // 1970-01-01 was a Thursday. |
22 | | static constexpr std::array<std::string_view, 12> months{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; |
23 | | |
24 | 0 | void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } |
25 | | |
26 | | static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing |
27 | | std::atomic<bool> g_used_system_time{false}; |
28 | | static std::atomic<MockableSteadyClock::mock_time_point::duration> g_mock_steady_time{}; //!< For testing |
29 | | |
30 | | NodeClock::time_point NodeClock::now() noexcept |
31 | 0 | { |
32 | 0 | const auto mocktime{g_mock_time.load(std::memory_order_relaxed)}; |
33 | 0 | if (!mocktime.count()) { |
34 | 0 | g_used_system_time = true; |
35 | 0 | } |
36 | 0 | const auto ret{ |
37 | 0 | mocktime.count() ? |
38 | 0 | mocktime : |
39 | 0 | std::chrono::system_clock::now().time_since_epoch()}; |
40 | 0 | assert(ret > 0s); |
41 | 0 | return time_point{ret}; |
42 | 0 | }; |
43 | | |
44 | 0 | void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); } |
45 | 0 | void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock) { SetMockTime(mock.time_since_epoch()); } |
46 | | void SetMockTime(std::chrono::seconds mock_time_in) |
47 | 24.9k | { |
48 | 24.9k | Assert(mock_time_in >= 0s); Line | Count | Source | 113 | 24.9k | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
49 | 24.9k | g_mock_time.store(mock_time_in, std::memory_order_relaxed); |
50 | 24.9k | } |
51 | | |
52 | | std::chrono::seconds GetMockTime() |
53 | 0 | { |
54 | 0 | return g_mock_time.load(std::memory_order_relaxed); |
55 | 0 | } |
56 | | |
57 | | MockableSteadyClock::time_point MockableSteadyClock::now() noexcept |
58 | 0 | { |
59 | 0 | const auto mocktime{g_mock_steady_time.load(std::memory_order_relaxed)}; |
60 | 0 | if (!mocktime.count()) { |
61 | 0 | g_used_system_time = true; |
62 | 0 | } |
63 | 0 | const auto ret{ |
64 | 0 | mocktime.count() ? |
65 | 0 | mocktime : |
66 | 0 | std::chrono::steady_clock::now().time_since_epoch()}; |
67 | 0 | return time_point{ret}; |
68 | 0 | }; |
69 | | |
70 | | void MockableSteadyClock::SetMockTime(mock_time_point::duration mock_time_in) |
71 | 0 | { |
72 | 0 | Assert(mock_time_in >= 0s); Line | Count | Source | 113 | 0 | #define Assert(val) inline_assertion_check<true>(val, std::source_location::current(), #val) |
|
73 | 0 | g_mock_steady_time.store(mock_time_in, std::memory_order_relaxed); |
74 | 0 | } |
75 | | |
76 | | void MockableSteadyClock::ClearMockTime() |
77 | 0 | { |
78 | 0 | g_mock_steady_time.store(0ms, std::memory_order_relaxed); |
79 | 0 | } |
80 | | |
81 | 0 | int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); } |
82 | | |
83 | | std::string FormatISO8601DateTime(int64_t nTime) |
84 | 0 | { |
85 | 0 | const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; |
86 | 0 | const auto days{std::chrono::floor<std::chrono::days>(secs)}; |
87 | 0 | const std::chrono::year_month_day ymd{days}; |
88 | 0 | const std::chrono::hh_mm_ss hms{secs - days}; |
89 | 0 | 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 | 0 | #define strprintf tfm::format |
|
90 | 0 | } |
91 | | |
92 | | std::string FormatISO8601Date(int64_t nTime) |
93 | 0 | { |
94 | 0 | const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; |
95 | 0 | const auto days{std::chrono::floor<std::chrono::days>(secs)}; |
96 | 0 | const std::chrono::year_month_day ymd{days}; |
97 | 0 | return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
98 | 0 | } |
99 | | |
100 | | std::optional<int64_t> ParseISO8601DateTime(std::string_view str) |
101 | 0 | { |
102 | 0 | constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()}; |
103 | 0 | if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') { |
104 | 0 | return {}; |
105 | 0 | } |
106 | 0 | const auto year{ToIntegral<uint16_t>(str.substr(0, 4))}; |
107 | 0 | const auto month{ToIntegral<uint8_t>(str.substr(5, 2))}; |
108 | 0 | const auto day{ToIntegral<uint8_t>(str.substr(8, 2))}; |
109 | 0 | const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))}; |
110 | 0 | const auto min{ToIntegral<uint8_t>(str.substr(14, 2))}; |
111 | 0 | const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))}; |
112 | 0 | if (!year || !month || !day || !hour || !min || !sec) { |
113 | 0 | return {}; |
114 | 0 | } |
115 | 0 | const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}}; |
116 | 0 | if (!ymd.ok()) { |
117 | 0 | return {}; |
118 | 0 | } |
119 | 0 | const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}}; |
120 | 0 | const auto tp{std::chrono::sys_days{ymd} + time}; |
121 | 0 | return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)}; |
122 | 0 | } |
123 | | |
124 | | std::string FormatRFC1123DateTime(int64_t time) |
125 | 0 | { |
126 | 0 | if (time < -62167219200 || 253402300799 < time) { |
127 | | // 4-digit year, so only support years 0 to 9999 |
128 | 0 | return ""; |
129 | 0 | } |
130 | 0 | const std::chrono::sys_seconds secs{std::chrono::seconds{time}}; |
131 | 0 | const auto days{std::chrono::floor<std::chrono::days>(secs)}; |
132 | 0 | const auto w{days.time_since_epoch().count() % 7}; // will be in the range [-6, 6] |
133 | 0 | std::string_view weekday{weekdays.at(w >= 0 ? w : w + 7)}; |
134 | 0 | const std::chrono::year_month_day ymd{days}; |
135 | 0 | std::string_view month{months.at(unsigned{ymd.month()} - 1)}; |
136 | 0 | const std::chrono::hh_mm_ss hms{secs - days}; |
137 | | // examples: Mon, 27 Jul 2009 12:28:53 GMT |
138 | | // Fri, 31 May 2024 19:18:04 GMT |
139 | 0 | return strprintf("%03s, %02u %03s %04i %02i:%02i:%02i GMT", weekday, unsigned{ymd.day()}, month, signed{ymd.year()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count());Line | Count | Source | 1172 | 0 | #define strprintf tfm::format |
|
140 | 0 | } |
141 | | |
142 | | struct timeval MillisToTimeval(int64_t nTimeout) |
143 | 0 | { |
144 | 0 | struct timeval timeout; |
145 | 0 | timeout.tv_sec = nTimeout / 1000; |
146 | 0 | timeout.tv_usec = (nTimeout % 1000) * 1000; |
147 | 0 | return timeout; |
148 | 0 | } |
149 | | |
150 | | struct timeval MillisToTimeval(std::chrono::milliseconds ms) |
151 | 0 | { |
152 | 0 | return MillisToTimeval(count_milliseconds(ms)); |
153 | 0 | } |