Bitcoin Core  21.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-2020 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 <stdint.h>
13 #include <string>
14 
15 using namespace std::chrono_literals;
16 
17 void UninterruptibleSleep(const std::chrono::microseconds& n);
18 
29 constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
30 constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
31 constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
32 
33 using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
34 
38 inline double CountSecondsDouble(SecondsDouble t) { return t.count(); }
39 
44 int64_t GetTime();
45 
47 int64_t GetTimeMillis();
49 int64_t GetTimeMicros();
51 int64_t GetSystemTimeInSeconds(); // Like GetTime(), but not mockable
52 
59 void SetMockTime(int64_t nMockTimeIn);
60 
62 void SetMockTime(std::chrono::seconds mock_time_in);
63 
65 std::chrono::seconds GetMockTime();
66 
68 template <typename T>
69 T GetTime();
70 
75 std::string FormatISO8601DateTime(int64_t nTime);
76 std::string FormatISO8601Date(int64_t nTime);
77 int64_t ParseISO8601DateTime(const std::string& str);
78 
82 struct timeval MillisToTimeval(int64_t nTimeout);
83 
87 struct timeval MillisToTimeval(std::chrono::milliseconds ms);
88 
90 bool ChronoSanityCheck();
91 
92 #endif // BITCOIN_UTIL_TIME_H
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
GetMockTime
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:112
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
FormatISO8601Date
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:145
CountSecondsDouble
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
Definition: time.h:38
ParseISO8601DateTime
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:158
GetTimeMillis
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
compat.h
SecondsDouble
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:33
GetSystemTimeInSeconds
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: time.cpp:127
GetTimeMicros
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:122
FormatISO8601DateTime
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:132
count_microseconds
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:31
ChronoSanityCheck
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
Definition: time.cpp:36
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
count_milliseconds
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:30
count_seconds
constexpr int64_t count_seconds(std::chrono::seconds t)
Helper to count the seconds of a duration.
Definition: time.h:29
MillisToTimeval
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:172