6#ifndef BITCOIN_UTIL_TIME_H
7#define BITCOIN_UTIL_TIME_H
15using namespace std::chrono_literals;
18struct NodeClock :
public std::chrono::system_clock {
49 return std::chrono::duration_cast<Dur1>(d).count();
51template <
typename Duration,
typename Timepo
int>
65using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
67 std::chrono::duration<double, std::chrono::seconds::period>;
109template <
typename T>
constexpr T
Now() {
110 return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
Implement std::hash so RCUPtr can be used as a key for maps or sets.
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
std::chrono::steady_clock SteadyClock
Mockable clock in the context of tests, otherwise the system clock.
static time_point now() noexcept
Return current system time or mocked time, if set.
std::chrono::time_point< NodeClock > time_point
static std::time_t to_time_t(const time_point &)=delete
static time_point from_time_t(std::time_t)=delete
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
int64_t GetTimeMicros()
Returns the system time (not mockable)
int64_t GetTimeMillis()
Returns the system time (not mockable)
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
constexpr T Now()
Return the current time point cast to the given precision.
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.
constexpr auto TicksSinceEpoch(Timepoint t)
std::string FormatISO8601Date(int64_t nTime)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
constexpr int64_t count_microseconds(std::chrono::microseconds t)
constexpr int64_t count_seconds(std::chrono::seconds t)
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
int64_t ParseISO8601DateTime(const std::string &str)
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds