Bitcoin Core 31.99.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// The `util/time.h` header is designed to be a drop-in replacement for `chrono`.
10#include <chrono> // IWYU pragma: export
11#include <cstdint>
12#include <ctime>
13#include <optional>
14#include <string>
15#include <string_view>
16
17#ifdef WIN32
18#include <winsock2.h>
19#else
20#include <sys/time.h>
21#endif
22
23using namespace std::chrono_literals;
24
27struct NodeClock : public std::chrono::system_clock {
28 using time_point = std::chrono::time_point<NodeClock>;
30 static time_point now() noexcept;
31 static std::time_t to_time_t(const time_point&) = delete; // unused
32 static time_point from_time_t(std::time_t) = delete; // unused
33};
34using NodeSeconds = std::chrono::time_point<NodeClock, std::chrono::seconds>;
35
36using SteadyClock = std::chrono::steady_clock;
37using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
38using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
39using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
40
41using SystemClock = std::chrono::system_clock;
42
46struct MockableSteadyClock : public std::chrono::steady_clock {
47 using time_point = std::chrono::time_point<MockableSteadyClock>;
48
49 using mock_time_point = std::chrono::time_point<MockableSteadyClock, std::chrono::milliseconds>;
50 static constexpr mock_time_point::duration INITIAL_MOCK_TIME{1};
51
53 static time_point now() noexcept;
54 static std::time_t to_time_t(const time_point&) = delete; // unused
55 static time_point from_time_t(std::time_t) = delete; // unused
56
62 static void SetMockTime(mock_time_point::duration mock_time_in);
63
65 static void ClearMockTime();
66};
67
68void UninterruptibleSleep(const std::chrono::microseconds& n);
69
80template <typename Dur1, typename Dur2>
81constexpr auto Ticks(Dur2 d)
82{
83 return std::chrono::duration_cast<Dur1>(d).count();
84}
85
86template <typename Duration>
87constexpr int64_t TicksSeconds(Duration d)
88{
89 return int64_t{Ticks<std::chrono::seconds>(d)};
90}
91template <typename Duration, typename Timepoint>
92constexpr auto TicksSinceEpoch(Timepoint t)
93{
94 return Ticks<Duration>(t.time_since_epoch());
95}
96constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
97constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
98constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
99
100using HoursDouble = std::chrono::duration<double, std::chrono::hours::period>;
101using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
102using MillisecondsDouble = std::chrono::duration<double, std::chrono::milliseconds::period>;
103
112int64_t GetTime();
113
120void SetMockTime(int64_t nMockTimeIn);
121
123void SetMockTime(std::chrono::seconds mock_time_in);
124void SetMockTime(std::chrono::time_point<NodeClock, std::chrono::seconds> mock);
125
127std::chrono::seconds GetMockTime();
128
133template <typename T>
135{
136 return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
137}
139template <typename T>
141{
142 return Now<std::chrono::time_point<NodeClock, T>>().time_since_epoch();
143}
144
149std::string FormatISO8601DateTime(int64_t nTime);
150std::string FormatISO8601Date(int64_t nTime);
151std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
152
157std::string FormatRFC1123DateTime(int64_t nTime);
158
162struct timeval MillisToTimeval(int64_t nTimeout);
163
167struct timeval MillisToTimeval(std::chrono::milliseconds ms);
168
169#endif // BITCOIN_UTIL_TIME_H
Definition: common.h:30
Version of SteadyClock that is mockable in the context of tests (via SteadyClockContext,...
Definition: time.h:46
std::chrono::time_point< MockableSteadyClock, std::chrono::milliseconds > mock_time_point
Definition: time.h:49
std::chrono::time_point< MockableSteadyClock > time_point
Definition: time.h:47
Version of the system clock that is mockable in the context of tests (via NodeClockContext or SetMock...
Definition: time.h:27
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:36
std::chrono::time_point< NodeClock > time_point
Definition: time.h:28
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:97
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:148
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:101
std::chrono::duration< double, std::chrono::hours::period > HoursDouble
Definition: time.h:100
std::chrono::steady_clock SteadyClock
Definition: time.h:36
std::chrono::duration< double, std::chrono::milliseconds::period > MillisecondsDouble
Definition: time.h:102
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:30
constexpr int64_t TicksSeconds(Duration d)
Definition: time.h:87
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:58
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
Definition: time.cpp:87
constexpr auto TicksSinceEpoch(Timepoint t)
Definition: time.h:92
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:98
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:50
T Now()
Return the current time point cast to the given precision.
Definition: time.h:134
std::chrono::system_clock SystemClock
Definition: time.h:41
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:98
constexpr int64_t count_seconds(std::chrono::seconds t)
Definition: time.h:96
std::chrono::time_point< std::chrono::steady_clock, std::chrono::microseconds > SteadyMicroseconds
Definition: time.h:39
std::chrono::time_point< NodeClock, std::chrono::seconds > NodeSeconds
Definition: time.h:34
std::optional< int64_t > ParseISO8601DateTime(std::string_view str)
Definition: time.cpp:106
std::chrono::time_point< std::chrono::steady_clock, std::chrono::seconds > SteadySeconds
Definition: time.h:37
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition: time.h:81
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:89
std::string FormatRFC1123DateTime(int64_t nTime)
RFC1123 formatting https://www.rfc-editor.org/rfc/rfc1123#section-5.2.14 Used in HTTP/1....
Definition: time.cpp:130
std::chrono::time_point< std::chrono::steady_clock, std::chrono::milliseconds > SteadyMilliseconds
Definition: time.h:38