diff --git a/src/util/time.cpp b/src/util/time.cpp index d3a3e00df..5d7fe0ecd 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -1,173 +1,176 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #if defined(HAVE_CONFIG_H) #include #endif #include #include #include #include #include #include #include void UninterruptibleSleep(const std::chrono::microseconds &n) { std::this_thread::sleep_for(n); } //! For testing static std::atomic nMockTime(0); 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( 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 _WIN32 if (gmtime_s(&epoch, &time_val) != 0) { #else if (gmtime_r(&time_val, &epoch) == nullptr) { #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; } template T GetTime() { const std::chrono::seconds mocktime{ nMockTime.load(std::memory_order_relaxed)}; const auto ret{ mocktime.count() ? mocktime : std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch())}; assert(ret > 0s); return ret; } template std::chrono::seconds GetTime(); template std::chrono::milliseconds GetTime(); template std::chrono::microseconds GetTime(); template static T GetSystemTime() { const auto now = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()); assert(now.count() > 0); return now; } void SetMockTime(int64_t nMockTimeIn) { Assert(nMockTimeIn >= 0); nMockTime.store(nMockTimeIn, std::memory_order_relaxed); } +void SetMockTime(std::chrono::seconds mock_time_in) { + nMockTime.store(mock_time_in.count(), std::memory_order_relaxed); +} std::chrono::seconds GetMockTime() { return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed)); } int64_t GetTimeMillis() { return int64_t{GetSystemTime().count()}; } int64_t GetTimeMicros() { return int64_t{GetSystemTime().count()}; } int64_t GetTimeSeconds() { return int64_t{GetSystemTime().count()}; } int64_t GetTime() { return GetTime().count(); } std::string FormatISO8601DateTime(int64_t nTime) { struct tm ts; time_t time_val = nTime; #ifdef _WIN32 if (gmtime_s(&ts, &time_val) != 0) { #else if (gmtime_r(&time_val, &ts) == nullptr) { #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 FormatISO8601Date(int64_t nTime) { struct tm ts; time_t time_val = nTime; #ifdef _WIN32 if (gmtime_s(&ts, &time_val) != 0) { #else if (gmtime_r(&time_val, &ts) == nullptr) { #endif return {}; } return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday); } int64_t ParseISO8601DateTime(const std::string &str) { static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0); static const std::locale loc( std::locale::classic(), new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ")); std::istringstream iss(str); iss.imbue(loc); boost::posix_time::ptime ptime(boost::date_time::not_a_date_time); iss >> ptime; if (ptime.is_not_a_date_time() || epoch > ptime) { return 0; } return (ptime - epoch).total_seconds(); } struct timeval MillisToTimeval(int64_t nTimeout) { struct timeval timeout; timeout.tv_sec = nTimeout / 1000; timeout.tv_usec = (nTimeout % 1000) * 1000; return timeout; } struct timeval MillisToTimeval(std::chrono::milliseconds ms) { return MillisToTimeval(count_milliseconds(ms)); } diff --git a/src/util/time.h b/src/util/time.h index 6103bd9a2..50ba9710a 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -1,93 +1,101 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto // Copyright (c) 2009-2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_UTIL_TIME_H #define BITCOIN_UTIL_TIME_H #include #include #include #include using namespace std::chrono_literals; void UninterruptibleSleep(const std::chrono::microseconds &n); /** * Helper to count the seconds of a duration. * * All durations should be using std::chrono and calling this should generally * be avoided in code. Though, it is still preferred to an inline t.count() to * protect against a reliance on the exact type of t. * * This helper is used to convert durations before passing them over an * interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI) */ constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); } constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); } constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); } using SecondsDouble = std::chrono::duration; /** * Helper to count the seconds in any std::chrono::duration type */ inline double CountSecondsDouble(SecondsDouble t) { return t.count(); } /** * DEPRECATED * Use either GetTimeSeconds (not mockable) or GetTime (mockable) */ int64_t GetTime(); /** Returns the system time (not mockable) */ int64_t GetTimeMillis(); /** Returns the system time (not mockable) */ int64_t GetTimeMicros(); /** Returns the system time (not mockable) */ // Like GetTime(), but not mockable int64_t GetTimeSeconds(); -/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ +/** + * DEPRECATED + * Use SetMockTime with chrono type + * + * @param[in] nMockTimeIn Time in seconds. + */ void SetMockTime(int64_t nMockTimeIn); +/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */ +void SetMockTime(std::chrono::seconds mock_time_in); + /** For testing */ std::chrono::seconds GetMockTime(); /** Return system time (or mocked time, if set) */ template T GetTime(); /** * ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date} * helper functions if possible. */ std::string FormatISO8601DateTime(int64_t nTime); std::string FormatISO8601Date(int64_t nTime); int64_t ParseISO8601DateTime(const std::string &str); /** * Convert milliseconds to a struct timeval for e.g. select. */ struct timeval MillisToTimeval(int64_t nTimeout); /** * Convert milliseconds to a struct timeval for e.g. select. */ struct timeval MillisToTimeval(std::chrono::milliseconds ms); /** Sanity check epoch match normal Unix epoch */ bool ChronoSanityCheck(); #endif // BITCOIN_UTIL_TIME_H