Bitcoin ABC 0.33.1
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 <compat/compat.h>
10
11#include <chrono>
12#include <cstdint>
13#include <optional>
14#include <string>
15#include <string_view>
16
17using namespace std::chrono_literals;
18
20struct NodeClock : public std::chrono::system_clock {
21 using time_point = std::chrono::time_point<NodeClock>;
23 static time_point now() noexcept;
24 static std::time_t to_time_t(const time_point &) = delete; // unused
25 static time_point from_time_t(std::time_t) = delete; // unused
26};
27using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
28
29using SteadyClock = std::chrono::steady_clock;
31 std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
32using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock,
33 std::chrono::milliseconds>;
34using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock,
35 std::chrono::microseconds>;
36
37using SystemClock = std::chrono::system_clock;
38
43struct MockableSteadyClock : public std::chrono::steady_clock {
44 using time_point = std::chrono::time_point<MockableSteadyClock>;
45
46 static constexpr std::chrono::milliseconds INITIAL_MOCK_TIME{1};
47
49 static time_point now() noexcept;
50 static std::time_t to_time_t(const time_point &) = delete; // unused
51 static time_point from_time_t(std::time_t) = delete; // unused
52
59 static void SetMockTime(std::chrono::milliseconds mock_time_in);
60
62 static void ClearMockTime();
63};
64
65void UninterruptibleSleep(const std::chrono::microseconds &n);
66
78template <typename Dur1, typename Dur2> constexpr auto Ticks(Dur2 d) {
79 return std::chrono::duration_cast<Dur1>(d).count();
80}
81template <typename Duration, typename Timepoint>
82constexpr auto TicksSinceEpoch(Timepoint t) {
83 return Ticks<Duration>(t.time_since_epoch());
84}
85constexpr int64_t count_seconds(std::chrono::seconds t) {
86 return t.count();
87}
88constexpr int64_t count_milliseconds(std::chrono::milliseconds t) {
89 return t.count();
90}
91constexpr int64_t count_microseconds(std::chrono::microseconds t) {
92 return t.count();
93}
94
95using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
97 std::chrono::duration<double, std::chrono::seconds::period>;
99 std::chrono::duration<double, std::chrono::milliseconds::period>;
100
105 return t.count();
106}
107
116int64_t GetTime();
117
119int64_t GetTimeMillis();
120
127void SetMockTime(int64_t nMockTimeIn);
128
130void SetMockTime(std::chrono::seconds mock_time_in);
131
133std::chrono::seconds GetMockTime();
134
139template <typename T> constexpr T Now() {
140 return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
141}
143template <typename T> T GetTime() {
144 return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
145}
146
151std::string FormatISO8601DateTime(int64_t nTime);
152std::string FormatISO8601Date(int64_t nTime);
153std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
154
158struct timeval MillisToTimeval(int64_t nTimeout);
159
163struct timeval MillisToTimeval(std::chrono::milliseconds ms);
164
165#endif // BITCOIN_UTIL_TIME_H
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:259
std::chrono::steady_clock SteadyClock
Definition: server.cpp:30
Version of SteadyClock that is mockable in the context of tests (set the current value with SetMockTi...
Definition: time.h:43
std::chrono::time_point< MockableSteadyClock > time_point
Definition: time.h:44
Mockable clock in the context of tests, otherwise the system clock.
Definition: time.h:20
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:29
std::chrono::time_point< NodeClock > time_point
Definition: time.h:21
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:88
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:148
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:76
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:97
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Definition: time.h:95
constexpr T Now()
Return the current time point cast to the given precision.
Definition: time.h:139
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:99
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:21
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:72
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:80
constexpr auto TicksSinceEpoch(Timepoint t)
Definition: time.h:82
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:110
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:64
std::chrono::system_clock SystemClock
Definition: time.h:37
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:91
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:85
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
Definition: time.h:35
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:27
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
Definition: time.h:104
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
Definition: time.cpp:121
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
Definition: time.h:31
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition: time.h:78
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:96
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition: time.h:33