6 #if defined(HAVE_CONFIG_H) 15 #include <boost/date_time/posix_time/posix_time.hpp> 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>(clock.time_since_epoch()).
count() != 0) {
46 time_t time_val = std::chrono::system_clock::to_time_t(clock);
47 if (time_val != time_t_epoch) {
54 if (gmtime_r(&time_val, &epoch) ==
nullptr) {
56 if (gmtime_s(&epoch, &time_val) != 0) {
61 if ((epoch.tm_sec != 0) ||
62 (epoch.tm_min != 0) ||
63 (epoch.tm_hour != 0) ||
64 (epoch.tm_mday != 1) ||
65 (epoch.tm_mon != 0) ||
66 (epoch.tm_year != 70)) {
74 const std::chrono::seconds mocktime{
nMockTime.load(std::memory_order_relaxed)};
78 std::chrono::system_clock::now().time_since_epoch()};
86 const auto now = std::chrono::duration_cast<
T>(std::chrono::system_clock::now().time_since_epoch());
94 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
99 nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
104 return std::chrono::seconds(
nMockTime.load(std::memory_order_relaxed));
109 return int64_t{GetSystemTime<std::chrono::milliseconds>().
count()};
114 return int64_t{GetSystemTime<std::chrono::microseconds>().
count()};
121 time_t time_val = nTime;
123 if (gmtime_r(&time_val, &ts) ==
nullptr) {
125 if (gmtime_s(&ts, &time_val) != 0) {
129 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);
134 time_t time_val = nTime;
136 if (gmtime_r(&time_val, &ts) ==
nullptr) {
138 if (gmtime_s(&ts, &time_val) != 0) {
142 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
147 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
148 static const std::locale loc(std::locale::classic(),
149 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
150 std::istringstream iss(str);
152 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
154 if (ptime.is_not_a_date_time() || epoch > ptime)
156 return (ptime - epoch).total_seconds();
161 struct timeval timeout;
162 timeout.tv_sec = nTimeout / 1000;
163 timeout.tv_usec = (nTimeout % 1000) * 1000;
std::chrono::time_point< NodeClock > time_point
std::chrono::seconds GetMockTime()
For testing.
std::string FormatISO8601Date(int64_t nTime)
int64_t GetTimeMillis()
Returns the system time (not mockable)
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
static std::atomic< int64_t > nMockTime(0)
For testing.
int64_t ParseISO8601DateTime(const std::string &str)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
int64_t GetTimeMicros()
Returns the system time (not mockable)
static time_point now() noexcept
Return current system time or mocked time, if set.
void UninterruptibleSleep(const std::chrono::microseconds &n)
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
int64_t GetTime()
DEPRECATED, see GetTime.
#define Assert(val)
Identity function.