aboutsummaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorfanquake <fanquake@gmail.com>2024-04-05 16:56:49 +0100
committerfanquake <fanquake@gmail.com>2024-04-05 17:02:00 +0100
commitc3530254c926a5ab9b710512bbbd29e5cd5b10f7 (patch)
tree0fca9ef37b2a7e59acaedb45340197b1558fcea8 /src/util
parenteb78ebc064b8d7fdd605e184efb6b45a53b2c6d5 (diff)
parentfa9f36babaceba6ab2f88e64bc4bc2956f58871f (diff)
downloadbitcoin-c3530254c926a5ab9b710512bbbd29e5cd5b10f7.tar.xz
Merge bitcoin/bitcoin#29081: refactor: Remove gmtime*
fa9f36babaceba6ab2f88e64bc4bc2956f58871f build: Remove HAVE_GMTIME_R (MarcoFalke) fa72dcbfa56177ca878375bae7c7bca6ca6a1f40 refactor: FormatISO8601* without gmtime* (MarcoFalke) fa2c486afc8501f2678cc19c9e9518a23c4ebcbd Revert "time: add runtime sanity check" (MarcoFalke) Pull request description: Now that the `ChronoSanityCheck` has passed for everyone with C++17 and is guaranteed by C++20 to always pass, remove it. Also, remove `gmtime_r` and `gmtime_s` and replace them with `year_month_day`+`hh_mm_ss` from C++20. ACKs for top commit: sipa: utACK fa9f36babaceba6ab2f88e64bc4bc2956f58871f fanquake: ACK fa9f36babaceba6ab2f88e64bc4bc2956f58871f - more std lib & even less stuff to port. Tree-SHA512: a9e7e805b757b7dade0bcc3f95273a7dc4f68622630d74838339789dd203ad7542d36b2e090a93b2bc5a7ecc383207dd7ec82c68147108bdac7ce44f088c8c9a
Diffstat (limited to 'src/util')
-rw-r--r--src/util/time.cpp88
-rw-r--r--src/util/time.h3
2 files changed, 15 insertions, 76 deletions
diff --git a/src/util/time.cpp b/src/util/time.cpp
index 5ca9d21f8d..456662bd84 100644
--- a/src/util/time.cpp
+++ b/src/util/time.cpp
@@ -3,70 +3,21 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
-#if defined(HAVE_CONFIG_H)
-#include <config/bitcoin-config.h>
-#endif
+#include <util/time.h>
#include <compat/compat.h>
#include <tinyformat.h>
-#include <util/time.h>
#include <util/check.h>
#include <atomic>
#include <chrono>
-#include <ctime>
-#include <locale>
-#include <thread>
-#include <sstream>
#include <string>
+#include <thread>
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
static std::atomic<int64_t> nMockTime(0); //!< For testing
-bool ChronoSanityCheck()
-{
- // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
- // to use the Unix epoch timestamp, prior to C++20, but in practice they almost
- // certainly will. Any differing behavior will be assumed to be an error, unless
- // certain platforms prove to consistently deviate, at which point we'll cope
- // with it by adding offsets.
-
- // Create a new clock from time_t(0) and make sure that it represents 0
- // seconds from the system_clock's time_since_epoch. Then convert that back
- // to a time_t and verify that it's the same as before.
- const time_t time_t_epoch{};
- auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
- if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
- return false;
- }
-
- time_t time_val = std::chrono::system_clock::to_time_t(clock);
- if (time_val != time_t_epoch) {
- return false;
- }
-
- // Check that the above zero time is actually equal to the known unix timestamp.
- struct tm epoch;
-#ifdef HAVE_GMTIME_R
- if (gmtime_r(&time_val, &epoch) == nullptr) {
-#else
- if (gmtime_s(&epoch, &time_val) != 0) {
-#endif
- return false;
- }
-
- if ((epoch.tm_sec != 0) ||
- (epoch.tm_min != 0) ||
- (epoch.tm_hour != 0) ||
- (epoch.tm_mday != 1) ||
- (epoch.tm_mon != 0) ||
- (epoch.tm_year != 70)) {
- return false;
- }
- return true;
-}
-
NodeClock::time_point NodeClock::now() noexcept
{
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
@@ -96,30 +47,21 @@ std::chrono::seconds GetMockTime()
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
-std::string FormatISO8601DateTime(int64_t nTime) {
- struct tm ts;
- time_t time_val = nTime;
-#ifdef HAVE_GMTIME_R
- if (gmtime_r(&time_val, &ts) == nullptr) {
-#else
- if (gmtime_s(&ts, &time_val) != 0) {
-#endif
- return {};
- }
- 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);
+std::string FormatISO8601DateTime(int64_t nTime)
+{
+ const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
+ const auto days{std::chrono::floor<std::chrono::days>(secs)};
+ const std::chrono::year_month_day ymd{days};
+ const std::chrono::hh_mm_ss hms{secs - days};
+ 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());
}
-std::string FormatISO8601Date(int64_t nTime) {
- struct tm ts;
- time_t time_val = nTime;
-#ifdef HAVE_GMTIME_R
- if (gmtime_r(&time_val, &ts) == nullptr) {
-#else
- if (gmtime_s(&ts, &time_val) != 0) {
-#endif
- return {};
- }
- return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
+std::string FormatISO8601Date(int64_t nTime)
+{
+ const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
+ const auto days{std::chrono::floor<std::chrono::days>(secs)};
+ const std::chrono::year_month_day ymd{days};
+ return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
}
struct timeval MillisToTimeval(int64_t nTimeout)
diff --git a/src/util/time.h b/src/util/time.h
index 6aa776137c..108560e0e0 100644
--- a/src/util/time.h
+++ b/src/util/time.h
@@ -116,7 +116,4 @@ struct timeval MillisToTimeval(int64_t nTimeout);
*/
struct timeval MillisToTimeval(std::chrono::milliseconds ms);
-/** Sanity check epoch match normal Unix epoch */
-bool ChronoSanityCheck();
-
#endif // BITCOIN_UTIL_TIME_H