6 #if defined(HAVE_CONFIG_H)
13 #include <boost/date_time/posix_time/posix_time.hpp>
25 int64_t mocktime =
nMockTime.load(std::memory_order_relaxed);
26 if (mocktime)
return mocktime;
28 time_t now = time(
nullptr);
36 const std::chrono::seconds mocktime{
nMockTime.load(std::memory_order_relaxed)};
38 return std::chrono::duration_cast<T>(
43 template std::chrono::seconds
GetTime();
44 template std::chrono::milliseconds
GetTime();
45 template std::chrono::microseconds
GetTime();
49 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
54 return nMockTime.load(std::memory_order_relaxed);
59 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
60 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
67 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
68 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
80 time_t time_val = nTime;
82 if (gmtime_r(&time_val, &ts) ==
nullptr) {
84 if (gmtime_s(&ts, &time_val) != 0) {
88 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);
93 time_t time_val = nTime;
95 if (gmtime_r(&time_val, &ts) ==
nullptr) {
97 if (gmtime_s(&ts, &time_val) != 0) {
101 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
106 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
107 static const std::locale loc(std::locale::classic(),
108 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
109 std::istringstream iss(str);
111 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
113 if (ptime.is_not_a_date_time() || epoch > ptime)
115 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 unit 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)