8#if defined(HAVE_CONFIG_H)
9#include <config/bitcoin-config.h>
15#include <boost/date_time/posix_time/posix_time.hpp>
24 std::this_thread::sleep_for(n);
40 const time_t time_t_epoch{};
41 auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
42 if (std::chrono::duration_cast<std::chrono::seconds>(
43 clock.time_since_epoch())
48 time_t time_val = std::chrono::system_clock::to_time_t(clock);
49 if (time_val != time_t_epoch) {
57 if (gmtime_s(&epoch, &time_val) != 0) {
59 if (gmtime_r(&time_val, &epoch) ==
nullptr) {
64 if ((epoch.tm_sec != 0) || (epoch.tm_min != 0) || (epoch.tm_hour != 0) ||
65 (epoch.tm_mday != 1) || (epoch.tm_mon != 0) || (epoch.tm_year != 70)) {
72 const std::chrono::seconds mocktime{
73 nMockTime.load(std::memory_order_relaxed)};
75 const auto ret{mocktime.count()
77 : std::chrono::system_clock::now().time_since_epoch()};
83 const auto now = std::chrono::duration_cast<T>(
84 std::chrono::system_clock::now().time_since_epoch());
91 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
95 nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
98 return std::chrono::seconds(
nMockTime.load(std::memory_order_relaxed));
102 return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
106 return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
110 return GetTime<std::chrono::seconds>().count();
115 time_t time_val = nTime;
117 if (gmtime_s(&ts, &time_val) != 0) {
119 if (gmtime_r(&time_val, &ts) ==
nullptr) {
123 return strprintf(
"%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900,
124 ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min,
130 time_t time_val = nTime;
132 if (gmtime_s(&ts, &time_val) != 0) {
134 if (gmtime_r(&time_val, &ts) ==
nullptr) {
138 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1,
143 static const boost::posix_time::ptime epoch =
144 boost::posix_time::from_time_t(0);
145 static const std::locale loc(
146 std::locale::classic(),
147 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
148 std::istringstream iss(str);
150 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
152 if (ptime.is_not_a_date_time() || epoch > ptime) {
155 return (ptime - epoch).total_seconds();
159 struct timeval timeout;
160 timeout.tv_sec = nTimeout / 1000;
161 timeout.tv_usec = (nTimeout % 1000) * 1000;
#define Assert(val)
Identity function.
Implement std::hash so RCUPtr can be used as a key for maps or sets.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
int64_t GetTimeMicros()
Returns the system time (not mockable)
int64_t GetTimeMillis()
Returns the system time (not mockable)
void UninterruptibleSleep(const std::chrono::microseconds &n)
static std::atomic< int64_t > nMockTime(0)
For testing.
std::chrono::seconds GetMockTime()
For testing.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601Date(int64_t nTime)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
int64_t ParseISO8601DateTime(const std::string &str)
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)