Bitcoin ABC  0.26.3
P2P Digital Currency
time.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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 <compat.h>
10 
11 #include <chrono>
12 #include <cstdint>
13 #include <string>
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;
29  std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
30 using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock,
31  std::chrono::milliseconds>;
32 using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock,
33  std::chrono::microseconds>;
34 
35 void UninterruptibleSleep(const std::chrono::microseconds &n);
36 
48 template <typename Dur1, typename Dur2> constexpr auto Ticks(Dur2 d) {
49  return std::chrono::duration_cast<Dur1>(d).count();
50 }
51 template <typename Duration, typename Timepoint>
52 constexpr auto TicksSinceEpoch(Timepoint t) {
53  return Ticks<Duration>(t.time_since_epoch());
54 }
55 constexpr int64_t count_seconds(std::chrono::seconds t) {
56  return t.count();
57 }
58 constexpr int64_t count_milliseconds(std::chrono::milliseconds t) {
59  return t.count();
60 }
61 constexpr int64_t count_microseconds(std::chrono::microseconds t) {
62  return t.count();
63 }
64 
65 using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
67  std::chrono::duration<double, std::chrono::seconds::period>;
68 
73  return t.count();
74 }
75 
84 int64_t GetTime();
85 
87 int64_t GetTimeMillis();
89 int64_t GetTimeMicros();
90 
97 void SetMockTime(int64_t nMockTimeIn);
98 
100 void SetMockTime(std::chrono::seconds mock_time_in);
101 
103 std::chrono::seconds GetMockTime();
104 
109 template <typename T> constexpr T Now() {
110  return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
111 }
113 template <typename T> T GetTime() {
114  return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
115 }
116 
121 std::string FormatISO8601DateTime(int64_t nTime);
122 std::string FormatISO8601Date(int64_t nTime);
123 int64_t ParseISO8601DateTime(const std::string &str);
124 
128 struct timeval MillisToTimeval(int64_t nTimeout);
129 
133 struct timeval MillisToTimeval(std::chrono::milliseconds ms);
134 
136 bool ChronoSanityCheck();
137 
138 #endif // BITCOIN_UTIL_TIME_H
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
std::chrono::steady_clock SteadyClock
Definition: server.cpp:26
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
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:58
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:158
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:105
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:101
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:67
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Definition: time.h:65
constexpr T Now()
Return the current time point cast to the given precision.
Definition: time.h:109
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:97
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.h:113
constexpr auto TicksSinceEpoch(Timepoint t)
Definition: time.h:52
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:128
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:89
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:61
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:55
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
Definition: time.h:33
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:25
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:142
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
Definition: time.h:72
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
Definition: time.h:29
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition: time.h:48
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
Definition: time.cpp:30
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:113
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition: time.h:31