7 #if defined(HAVE_CONFIG_H)
15 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include <boost/thread.hpp>
24 static std::atomic<int64_t> nMockTime(0);
29 int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
30 if (mocktime)
return mocktime;
32 time_t now = time(
nullptr);
40 const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
42 return std::chrono::duration_cast<T>(
45 std::chrono::microseconds{GetTimeMicros()});
47 template std::chrono::seconds
GetTime();
48 template std::chrono::milliseconds
GetTime();
49 template std::chrono::microseconds
GetTime();
53 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
58 return nMockTime.load(std::memory_order_relaxed);
63 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
64 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
76 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
77 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
84 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
89 int seconds = nDurationTime % 60;
91 int minutes = nDurationTime % 60;
93 int hours = nDurationTime % 24;
94 int days = nDurationTime / 24;
96 return strprintf(
"%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
98 return strprintf(
"%02dh:%02dm:%02ds", hours, minutes, seconds);
99 return strprintf(
"%02dm:%02ds", minutes, seconds);
104 time_t time_val = nTime;
106 if (gmtime_r(&time_val, &ts) ==
nullptr) {
108 if (gmtime_s(&ts, &time_val) != 0) {
112 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);
117 time_t time_val = nTime;
119 if (gmtime_r(&time_val, &ts) ==
nullptr) {
121 if (gmtime_s(&ts, &time_val) != 0) {
125 return strprintf(
".%04i%02i%02iT%02i%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min);
130 time_t time_val = nTime;
132 if (gmtime_r(&time_val, &ts) ==
nullptr) {
134 if (gmtime_s(&ts, &time_val) != 0) {
138 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
143 time_t time_val = nTime;
145 if (gmtime_r(&time_val, &ts) ==
nullptr) {
147 if (gmtime_s(&ts, &time_val) != 0) {
151 return strprintf(
"%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
#define T(expected, seed, data)
int64_t GetTimeMicros()
Returns the system time (not mockable)
int64_t GetTimeMillis()
Returns the system time (not mockable)
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
void UninterruptibleSleep(const std::chrono::microseconds &n)
std::string FormatISO8601Date(int64_t nTime)
std::string DurationToDHMS(int64_t nDurationTime)
void SetMockTime(int64_t nMockTimeIn)
For testing.
std::string FormatISO8601Time(int64_t nTime)
int64_t GetMockTime()
For testing.
std::string FormatISO8601DateTimeForBackup(int64_t nTime)
void MilliSleep(int64_t n)
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.