6 #if defined(HAVE_CONFIG_H)
16 #include <boost/date_time/posix_time/posix_time.hpp>
28 int64_t mocktime =
nMockTime.load(std::memory_order_relaxed);
29 if (mocktime)
return mocktime;
31 time_t now = time(
nullptr);
47 const time_t time_t_epoch{};
48 auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
49 if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
53 time_t time_val = std::chrono::system_clock::to_time_t(clock);
54 if (time_val != time_t_epoch) {
61 if (gmtime_r(&time_val, &epoch) ==
nullptr) {
63 if (gmtime_s(&epoch, &time_val) != 0) {
68 if ((epoch.tm_sec != 0) ||
69 (epoch.tm_min != 0) ||
70 (epoch.tm_hour != 0) ||
71 (epoch.tm_mday != 1) ||
72 (epoch.tm_mon != 0) ||
73 (epoch.tm_year != 70)) {
82 const std::chrono::seconds mocktime{
nMockTime.load(std::memory_order_relaxed)};
84 return std::chrono::duration_cast<T>(
87 std::chrono::microseconds{GetTimeMicros()});
89 template std::chrono::seconds
GetTime();
90 template std::chrono::milliseconds
GetTime();
91 template std::chrono::microseconds
GetTime();
96 const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
104 nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
109 nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
114 return std::chrono::seconds(
nMockTime.load(std::memory_order_relaxed));
119 return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
124 return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
129 return int64_t{GetSystemTime<std::chrono::seconds>().count()};
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-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
147 time_t time_val = nTime;
149 if (gmtime_r(&time_val, &ts) ==
nullptr) {
151 if (gmtime_s(&ts, &time_val) != 0) {
155 return strprintf(
"%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
160 static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
161 static const std::locale loc(std::locale::classic(),
162 new boost::posix_time::time_input_facet(
"%Y-%m-%dT%H:%M:%SZ"));
163 std::istringstream iss(str);
165 boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
167 if (ptime.is_not_a_date_time() || epoch > ptime)
169 return (ptime - epoch).total_seconds();
174 struct timeval timeout;
175 timeout.tv_sec = nTimeout / 1000;
176 timeout.tv_usec = (nTimeout % 1000) * 1000;