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)};
78 : std::chrono::duration_cast<T>(
79 std::chrono::system_clock::now().time_since_epoch())};
83 template std::chrono::seconds
GetTime();
84 template std::chrono::milliseconds
GetTime();
85 template std::chrono::microseconds
GetTime();
88 const auto now = std::chrono::duration_cast<T>(
89 std::chrono::system_clock::now().time_since_epoch());
96 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
100 nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
103 return std::chrono::seconds(
nMockTime.load(std::memory_order_relaxed));
107 return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
111 return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
115 return GetTime<std::chrono::seconds>().count();
120 time_t time_val = nTime;
122 if (gmtime_s(&ts, &time_val) != 0) {
124 if (gmtime_r(&time_val, &ts) ==
nullptr) {
128 return strprintf(
"%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900,
129 ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min,
135 time_t time_val = nTime;
137 if (gmtime_s(&ts, &time_val) != 0) {
139 if (gmtime_r(&time_val, &ts) ==
nullptr) {
143 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1,
148 static const boost::posix_time::ptime epoch =
149 boost::posix_time::from_time_t(0);
150 static const std::locale loc(
151 std::locale::classic(),
152 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
153 std::istringstream iss(str);
155 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
157 if (ptime.is_not_a_date_time() || epoch > ptime) {
160 return (ptime - epoch).total_seconds();
164 struct timeval timeout;
165 timeout.tv_sec = nTimeout / 1000;
166 timeout.tv_usec = (nTimeout % 1000) * 1000;
#define Assert(val)
Identity function.
static std::atomic< int64_t > nMockTime(0)
For testing.
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)
std::chrono::seconds GetMockTime()
For testing.
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.
T GetTime()
Return system time (or mocked time, if set)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)