diff options
author | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2023-12-14 18:20:48 +0100 |
---|---|---|
committer | MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> | 2024-03-18 16:01:24 +0100 |
commit | fa72dcbfa56177ca878375bae7c7bca6ca6a1f40 (patch) | |
tree | 38cea2fccc67c51002efca49d4bf9a58de20b8dc /src/util | |
parent | fa2c486afc8501f2678cc19c9e9518a23c4ebcbd (diff) | |
download | bitcoin-fa72dcbfa56177ca878375bae7c7bca6ca6a1f40.tar.xz |
refactor: FormatISO8601* without gmtime*
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/time.cpp | 45 |
1 files changed, 15 insertions, 30 deletions
diff --git a/src/util/time.cpp b/src/util/time.cpp index ccb1f50610..456662bd84 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -3,22 +3,16 @@ // 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); } @@ -53,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) |