27 const auto mocktime{
g_mock_time.load(std::memory_order_relaxed)};
28 if (!mocktime.count()) {
34 std::chrono::system_clock::now().time_since_epoch()};
43 g_mock_time.store(mock_time_in, std::memory_order_relaxed);
51int64_t
GetTime() {
return GetTime<std::chrono::seconds>().count(); }
55 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
56 const auto days{std::chrono::floor<std::chrono::days>(secs)};
57 const std::chrono::year_month_day ymd{days};
58 const std::chrono::hh_mm_ss hms{secs - days};
59 return strprintf(
"%04i-%02u-%02uT%02i:%02i:%02iZ",
signed{ymd.year()},
unsigned{ymd.month()},
unsigned{ymd.day()}, hms.hours().
count(), hms.minutes().count(), hms.seconds().count());
64 const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
65 const auto days{std::chrono::floor<std::chrono::days>(secs)};
66 const std::chrono::year_month_day ymd{days};
67 return strprintf(
"%04i-%02u-%02u",
signed{ymd.year()},
unsigned{ymd.month()},
unsigned{ymd.day()});
72 constexpr auto FMT_SIZE{std::string_view{
"2000-01-01T01:01:01Z"}.size()};
73 if (str.size() != FMT_SIZE || str[4] !=
'-' || str[7] !=
'-' || str[10] !=
'T' || str[13] !=
':' || str[16] !=
':' || str[19] !=
'Z') {
76 const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
77 const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
78 const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
79 const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
80 const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
81 const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
82 if (!year || !month || !day || !hour || !min || !sec) {
85 const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
89 const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
90 const auto tp{std::chrono::sys_days{ymd} + time};
91 return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
96 struct timeval timeout;
97 timeout.tv_sec = nTimeout / 1000;
98 timeout.tv_usec = (nTimeout % 1000) * 1000;
#define Assert(val)
Identity function.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
void UninterruptibleSleep(const std::chrono::microseconds &n)
std::chrono::seconds GetMockTime()
For testing.
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::string FormatISO8601Date(int64_t nTime)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
static std::atomic< std::chrono::seconds > g_mock_time
For testing.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
std::atomic< bool > g_used_system_time
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)