6 #ifndef BITCOIN_UTIL_TIME_H 7 #define BITCOIN_UTIL_TIME_H 13 #include <string_view> 18 struct NodeClock :
public std::chrono::system_clock {
39 using time_point = std::chrono::time_point<MockableSteadyClock>;
41 static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1};
44 static time_point now() noexcept;
45 static
std::time_t to_time_t(const time_point&) = delete;
46 static time_point from_time_t(
std::time_t) = delete;
56 static
void ClearMockTime();
71 template <typename Dur1, typename Dur2>
74 return std::chrono::duration_cast<Dur1>(d).
count();
76 template <
typename Duration,
typename Timepo
int>
79 return Ticks<Duration>(
t.time_since_epoch());
81 constexpr int64_t
count_seconds(std::chrono::seconds t) {
return t.count(); }
85 using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
86 using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
108 void SetMockTime(std::chrono::seconds mock_time_in);
117 template <
typename T>
120 return std::chrono::time_point_cast<
typename T::duration>(T::clock::now());
123 template <
typename T>
126 return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
147 #endif // BITCOIN_UTIL_TIME_H Mockable clock in the context of tests, otherwise the system clock.
std::chrono::time_point< NodeClock > time_point
T Now()
Return the current time point cast to the given precision.
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
constexpr auto TicksSinceEpoch(Timepoint t)
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.
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
std::chrono::steady_clock SteadyClock
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
constexpr int64_t count_microseconds(std::chrono::microseconds t)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
std::chrono::system_clock SystemClock
constexpr int64_t count_seconds(std::chrono::seconds t)
std::chrono::time_point< MockableSteadyClock > time_point
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Version of SteadyClock that is mockable in the context of tests (set the current value with SetMockTi...
std::string FormatISO8601Date(int64_t nTime)
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
#define T(expected, seed, data)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.