Bitcoin Core  29.1.0
P2P Digital Currency
time.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-present 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 <optional>
12 #include <string>
13 #include <string_view>
14 
15 using namespace std::chrono_literals;
16 
18 struct NodeClock : public std::chrono::system_clock {
19  using time_point = std::chrono::time_point<NodeClock>;
21  static time_point now() noexcept;
22  static std::time_t to_time_t(const time_point&) = delete; // unused
23  static time_point from_time_t(std::time_t) = delete; // unused
24 };
25 using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
26 
27 using SteadyClock = std::chrono::steady_clock;
28 using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
29 using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
30 using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
31 
32 using SystemClock = std::chrono::system_clock;
33 
38 struct MockableSteadyClock : public std::chrono::steady_clock {
39  using time_point = std::chrono::time_point<MockableSteadyClock>;
40 
41  static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1};
42 
44  static time_point now() noexcept;
45  static std::time_t to_time_t(const time_point&) = delete; // unused
46  static time_point from_time_t(std::time_t) = delete; // unused
47 
53  static void SetMockTime(std::chrono::milliseconds mock_time_in);
54 
56  static void ClearMockTime();
57 };
58 
59 void UninterruptibleSleep(const std::chrono::microseconds& n);
60 
71 template <typename Dur1, typename Dur2>
72 constexpr auto Ticks(Dur2 d)
73 {
74  return std::chrono::duration_cast<Dur1>(d).count();
75 }
76 template <typename Duration, typename Timepoint>
77 constexpr auto TicksSinceEpoch(Timepoint t)
78 {
79  return Ticks<Duration>(t.time_since_epoch());
80 }
81 constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
82 constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
83 constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
84 
85 using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
86 using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
87 using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>;
88 
97 int64_t GetTime();
98 
105 void SetMockTime(int64_t nMockTimeIn);
106 
108 void SetMockTime(std::chrono::seconds mock_time_in);
109 
111 std::chrono::seconds GetMockTime();
112 
117 template <typename T>
119 {
120  return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
121 }
123 template <typename T>
125 {
126  return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
127 }
128 
133 std::string FormatISO8601DateTime(int64_t nTime);
134 std::string FormatISO8601Date(int64_t nTime);
135 std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
136 
140 struct timeval MillisToTimeval(int64_t nTimeout);
141 
145 struct timeval MillisToTimeval(std::chrono::milliseconds ms);
146 
147 #endif // BITCOIN_UTIL_TIME_H
Mockable clock in the context of tests, otherwise the system clock.
Definition: time.h:18
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
T Now()
Return the current time point cast to the given precision.
Definition: time.h:118
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
Definition: time.cpp:95
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
Definition: time.h:28
constexpr auto TicksSinceEpoch(Timepoint t)
Definition: time.h:77
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:119
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:20
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:47
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
std::chrono::steady_clock SteadyClock
Definition: time.h:27
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:82
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:87
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:83
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.h:124
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition: time.h:29
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:86
std::chrono::system_clock SystemClock
Definition: time.h:32
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:81
std::chrono::time_point< MockableSteadyClock > time_point
Definition: time.h:39
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
Definition: time.h:30
static int count
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition: time.h:72
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Definition: time.h:85
Version of SteadyClock that is mockable in the context of tests (set the current value with SetMockTi...
Definition: time.h:38
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:87
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:78
#define T(expected, seed, data)
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:40