6 #if defined(HAVE_CONFIG_H)
15 #include <boost/date_time/posix_time/posix_time.hpp>
27 int64_t mocktime =
nMockTime.load(std::memory_order_relaxed);
28 if (mocktime)
return mocktime;
30 time_t now = time(
nullptr);
38 const std::chrono::seconds mocktime{
nMockTime.load(std::memory_order_relaxed)};
40 return std::chrono::duration_cast<T>(
45 template std::chrono::seconds
GetTime();
46 template std::chrono::milliseconds
GetTime();
47 template std::chrono::microseconds
GetTime();
52 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
57 return nMockTime.load(std::memory_order_relaxed);
62 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
63 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
70 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
71 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
83 time_t time_val = nTime;
85 if (gmtime_r(&time_val, &ts) ==
nullptr) {
87 if (gmtime_s(&ts, &time_val) != 0) {
91 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);
96 time_t time_val = nTime;
98 if (gmtime_r(&time_val, &ts) ==
nullptr) {
100 if (gmtime_s(&ts, &time_val) != 0) {
104 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
109 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
110 static const std::locale loc(std::locale::classic(),
111 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
112 std::istringstream iss(str);
114 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
116 if (ptime.is_not_a_date_time() || epoch > ptime)
118 return (ptime - epoch).total_seconds();
std::string FormatISO8601Date(int64_t nTime)
int64_t GetTimeMillis()
Returns the system time (not mockable)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
static std::atomic< int64_t > nMockTime(0)
For testing.
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
int64_t ParseISO8601DateTime(const std::string &str)
void SetMockTime(int64_t nMockTimeIn)
For testing.
int64_t GetMockTime()
For testing.
int64_t GetTimeMicros()
Returns the system time (not mockable)
void UninterruptibleSleep(const std::chrono::microseconds &n)
int64_t GetTime()
Return system time (or mocked time, if set)
#define Assert(val)
Identity function.