|
3 | 3 | // Distributed under the MIT software license, see the accompanying
|
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 |
|
6 |
| -#if defined(HAVE_CONFIG_H) |
7 |
| -#include <config/bitcoin-config.h> |
8 |
| -#endif |
| 6 | +#include <util/time.h> |
9 | 7 |
|
10 | 8 | #include <compat/compat.h>
|
11 | 9 | #include <tinyformat.h>
|
12 |
| -#include <util/time.h> |
13 | 10 | #include <util/check.h>
|
14 | 11 |
|
15 | 12 | #include <atomic>
|
16 | 13 | #include <chrono>
|
17 |
| -#include <ctime> |
18 |
| -#include <locale> |
19 |
| -#include <thread> |
20 |
| -#include <sstream> |
21 | 14 | #include <string>
|
| 15 | +#include <thread> |
22 | 16 |
|
23 | 17 | void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
24 | 18 |
|
25 | 19 | static std::atomic<int64_t> nMockTime(0); //!< For testing
|
26 | 20 |
|
27 |
| -bool ChronoSanityCheck() |
28 |
| -{ |
29 |
| - // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed |
30 |
| - // to use the Unix epoch timestamp, prior to C++20, but in practice they almost |
31 |
| - // certainly will. Any differing behavior will be assumed to be an error, unless |
32 |
| - // certain platforms prove to consistently deviate, at which point we'll cope |
33 |
| - // with it by adding offsets. |
34 |
| - |
35 |
| - // Create a new clock from time_t(0) and make sure that it represents 0 |
36 |
| - // seconds from the system_clock's time_since_epoch. Then convert that back |
37 |
| - // to a time_t and verify that it's the same as before. |
38 |
| - const time_t time_t_epoch{}; |
39 |
| - auto clock = std::chrono::system_clock::from_time_t(time_t_epoch); |
40 |
| - if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) { |
41 |
| - return false; |
42 |
| - } |
43 |
| - |
44 |
| - time_t time_val = std::chrono::system_clock::to_time_t(clock); |
45 |
| - if (time_val != time_t_epoch) { |
46 |
| - return false; |
47 |
| - } |
48 |
| - |
49 |
| - // Check that the above zero time is actually equal to the known unix timestamp. |
50 |
| - struct tm epoch; |
51 |
| -#ifdef HAVE_GMTIME_R |
52 |
| - if (gmtime_r(&time_val, &epoch) == nullptr) { |
53 |
| -#else |
54 |
| - if (gmtime_s(&epoch, &time_val) != 0) { |
55 |
| -#endif |
56 |
| - return false; |
57 |
| - } |
58 |
| - |
59 |
| - if ((epoch.tm_sec != 0) || |
60 |
| - (epoch.tm_min != 0) || |
61 |
| - (epoch.tm_hour != 0) || |
62 |
| - (epoch.tm_mday != 1) || |
63 |
| - (epoch.tm_mon != 0) || |
64 |
| - (epoch.tm_year != 70)) { |
65 |
| - return false; |
66 |
| - } |
67 |
| - return true; |
68 |
| -} |
69 |
| - |
70 | 21 | NodeClock::time_point NodeClock::now() noexcept
|
71 | 22 | {
|
72 | 23 | const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
|
@@ -96,30 +47,21 @@ std::chrono::seconds GetMockTime()
|
96 | 47 |
|
97 | 48 | int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
98 | 49 |
|
99 |
| -std::string FormatISO8601DateTime(int64_t nTime) { |
100 |
| - struct tm ts; |
101 |
| - time_t time_val = nTime; |
102 |
| -#ifdef HAVE_GMTIME_R |
103 |
| - if (gmtime_r(&time_val, &ts) == nullptr) { |
104 |
| -#else |
105 |
| - if (gmtime_s(&ts, &time_val) != 0) { |
106 |
| -#endif |
107 |
| - return {}; |
108 |
| - } |
109 |
| - return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec); |
| 50 | +std::string FormatISO8601DateTime(int64_t nTime) |
| 51 | +{ |
| 52 | + const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; |
| 53 | + const auto days{std::chrono::floor<std::chrono::days>(secs)}; |
| 54 | + const std::chrono::year_month_day ymd{days}; |
| 55 | + const std::chrono::hh_mm_ss hms{secs - days}; |
| 56 | + 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()); |
110 | 57 | }
|
111 | 58 |
|
112 |
| -std::string FormatISO8601Date(int64_t nTime) { |
113 |
| - struct tm ts; |
114 |
| - time_t time_val = nTime; |
115 |
| -#ifdef HAVE_GMTIME_R |
116 |
| - if (gmtime_r(&time_val, &ts) == nullptr) { |
117 |
| -#else |
118 |
| - if (gmtime_s(&ts, &time_val) != 0) { |
119 |
| -#endif |
120 |
| - return {}; |
121 |
| - } |
122 |
| - return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday); |
| 59 | +std::string FormatISO8601Date(int64_t nTime) |
| 60 | +{ |
| 61 | + const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}}; |
| 62 | + const auto days{std::chrono::floor<std::chrono::days>(secs)}; |
| 63 | + const std::chrono::year_month_day ymd{days}; |
| 64 | + return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}); |
123 | 65 | }
|
124 | 66 |
|
125 | 67 | struct timeval MillisToTimeval(int64_t nTimeout)
|
|
0 commit comments