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 
17 void UninterruptibleSleep(const std::chrono::microseconds &n);
18 
29 constexpr int64_t count_seconds(std::chrono::seconds t) {
30  return t.count();
31 }
32 constexpr int64_t count_milliseconds(std::chrono::milliseconds t) {
33  return t.count();
34 }
35 constexpr int64_t count_microseconds(std::chrono::microseconds t) {
36  return t.count();
37 }
38 
40  std::chrono::duration<double, std::chrono::seconds::period>;
41 
46  return t.count();
47 }
48 
53 int64_t GetTime();
54 
56 int64_t GetTimeMillis();
58 int64_t GetTimeMicros();
60 // Like GetTime(), but not mockable
61 int64_t GetTimeSeconds();
62 
69 void SetMockTime(int64_t nMockTimeIn);
70 
72 void SetMockTime(std::chrono::seconds mock_time_in);
73 
75 std::chrono::seconds GetMockTime();
76 
78 template <typename T> T GetTime();
79 
84 std::string FormatISO8601DateTime(int64_t nTime);
85 std::string FormatISO8601Date(int64_t nTime);
86 int64_t ParseISO8601DateTime(const std::string &str);
87 
91 struct timeval MillisToTimeval(int64_t nTimeout);
92 
96 struct timeval MillisToTimeval(std::chrono::milliseconds ms);
97 
99 bool ChronoSanityCheck();
100 
101 #endif // BITCOIN_UTIL_TIME_H
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:32
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:167
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:110
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:106
std::chrono::duration< double, std::chrono::seconds::period > SecondsDouble
Definition: time.h:40
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:102
int64_t GetTime()
DEPRECATED Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:71
int64_t GetTimeSeconds()
Returns the system time (not mockable)
Definition: time.cpp:114
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:137
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:94
constexpr int64_t count_microseconds(std::chrono::microseconds t)
Definition: time.h:35
constexpr int64_t count_seconds(std::chrono::seconds t)
Helper to count the seconds of a duration.
Definition: time.h:29
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:151
double CountSecondsDouble(SecondsDouble t)
Helper to count the seconds in any std::chrono::duration type.
Definition: time.h:45
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:122