Bitcoin Core 28.0.0
P2P Digital Currency
Loading...
Searching...
No Matches
time.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2022 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#ifndef BITCOIN_UTIL_TIME_H
7#define BITCOIN_UTIL_TIME_H
8
9#include <chrono> // IWYU pragma: export
10#include <cstdint>
11#include <string>
12
13using namespace std::chrono_literals;
14
16struct NodeClock : public std::chrono::system_clock {
17 using time_point = std::chrono::time_point<NodeClock>;
19 static time_point now() noexcept;
20 static std::time_t to_time_t(const time_point&) = delete; // unused
21 static time_point from_time_t(std::time_t) = delete; // unused
22};
23using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
24
25using SteadyClock = std::chrono::steady_clock;
26using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
27using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
28using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
29
30using SystemClock = std::chrono::system_clock;
31
32void UninterruptibleSleep(const std::chrono::microseconds& n);
33
44template <typename Dur1, typename Dur2>
45constexpr auto Ticks(Dur2 d)
46{
47 return std::chrono::duration_cast<Dur1>(d).count();
48}
49template <typename Duration, typename Timepoint>
50constexpr auto TicksSinceEpoch(Timepoint t)
51{
52 return Ticks<Duration>(t.time_since_epoch());
53}
54constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
55constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
56constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
57
58using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
59using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
60using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>;
61
70int64_t GetTime();
71
78void SetMockTime(int64_t nMockTimeIn);
79
81void SetMockTime(std::chrono::seconds mock_time_in);
82
84std::chrono::seconds GetMockTime();
85
90template <typename T>
92{
93 return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
94}
96template <typename T>
98{
99 return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
100}
101
106std::string FormatISO8601DateTime(int64_t nTime);
107std::string FormatISO8601Date(int64_t nTime);
108
112struct timeval MillisToTimeval(int64_t nTimeout);
113
117struct timeval MillisToTimeval(std::chrono::milliseconds ms);
118
119#endif // BITCOIN_UTIL_TIME_H
#define T(expected, seed, data)
Mockable clock in the context of tests, otherwise the system clock.
Definition time.h:16
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition time.cpp:21
std::chrono::time_point< NodeClock > time_point
Definition time.h:17
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)
Definition time.h:55
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition time.cpp:63
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition time.h:59
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Definition time.h:58
std::chrono::steady_clock SteadyClock
Definition time.h:25
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition time.h:60
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition time.cpp:17
std::chrono::seconds GetMockTime()
For testing.
Definition time.cpp:39
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition time.cpp:44
constexpr auto TicksSinceEpoch(Timepoint t)
Definition time.h:50
std::string FormatISO8601Date(int64_t nTime)
Definition time.cpp:55
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition time.cpp:32
T Now()
Return the current time point cast to the given precision.
Definition time.h:91
std::chrono::system_clock SystemClock
Definition time.h:30
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition time.h:56
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition time.h:54
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
Definition time.h:28
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition time.h:23
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
Definition time.h:26
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:45
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition time.cpp:46
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition time.h:27