Bitcoin Core  24.99.0
P2P Digital Currency
time.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 #if defined(HAVE_CONFIG_H)
8 #endif
9 
10 #include <compat/compat.h>
11 #include <tinyformat.h>
12 #include <util/time.h>
13 #include <util/check.h>
14 
15 #include <atomic>
16 #include <chrono>
17 #include <ctime>
18 #include <locale>
19 #include <thread>
20 #include <sstream>
21 #include <string>
22 
23 void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
24 
25 static std::atomic<int64_t> nMockTime(0);
26 
28 {
29  // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
30  // to use the Unix epoch timestamp, prior to C++20, but in practice they almost
31  // certainly will. Any differing behavior will be assumed to be an error, unless
32  // certain platforms prove to consistently deviate, at which point we'll cope
33  // with it by adding offsets.
34 
35  // Create a new clock from time_t(0) and make sure that it represents 0
36  // seconds from the system_clock's time_since_epoch. Then convert that back
37  // to a time_t and verify that it's the same as before.
38  const time_t time_t_epoch{};
39  auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
40  if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
41  return false;
42  }
43 
44  time_t time_val = std::chrono::system_clock::to_time_t(clock);
45  if (time_val != time_t_epoch) {
46  return false;
47  }
48 
49  // Check that the above zero time is actually equal to the known unix timestamp.
50  struct tm epoch;
51 #ifdef HAVE_GMTIME_R
52  if (gmtime_r(&time_val, &epoch) == nullptr) {
53 #else
54  if (gmtime_s(&epoch, &time_val) != 0) {
55 #endif
56  return false;
57  }
58 
59  if ((epoch.tm_sec != 0) ||
60  (epoch.tm_min != 0) ||
61  (epoch.tm_hour != 0) ||
62  (epoch.tm_mday != 1) ||
63  (epoch.tm_mon != 0) ||
64  (epoch.tm_year != 70)) {
65  return false;
66  }
67  return true;
68 }
69 
71 {
72  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
73  const auto ret{
74  mocktime.count() ?
75  mocktime :
76  std::chrono::system_clock::now().time_since_epoch()};
77  assert(ret > 0s);
78  return time_point{ret};
79 };
80 
81 template <typename T>
82 static T GetSystemTime()
83 {
84  const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
85  assert(now.count() > 0);
86  return now;
87 }
88 
89 void SetMockTime(int64_t nMockTimeIn)
90 {
91  Assert(nMockTimeIn >= 0);
92  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
93 }
94 
95 void SetMockTime(std::chrono::seconds mock_time_in)
96 {
97  nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
98 }
99 
100 std::chrono::seconds GetMockTime()
101 {
102  return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
103 }
104 
105 int64_t GetTimeMillis()
106 {
107  return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
108 }
109 
110 int64_t GetTimeMicros()
111 {
112  return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
113 }
114 
115 int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
116 
117 std::string FormatISO8601DateTime(int64_t nTime) {
118  struct tm ts;
119  time_t time_val = nTime;
120 #ifdef HAVE_GMTIME_R
121  if (gmtime_r(&time_val, &ts) == nullptr) {
122 #else
123  if (gmtime_s(&ts, &time_val) != 0) {
124 #endif
125  return {};
126  }
127  return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
128 }
129 
130 std::string FormatISO8601Date(int64_t nTime) {
131  struct tm ts;
132  time_t time_val = nTime;
133 #ifdef HAVE_GMTIME_R
134  if (gmtime_r(&time_val, &ts) == nullptr) {
135 #else
136  if (gmtime_s(&ts, &time_val) != 0) {
137 #endif
138  return {};
139  }
140  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
141 }
142 
143 struct timeval MillisToTimeval(int64_t nTimeout)
144 {
145  struct timeval timeout;
146  timeout.tv_sec = nTimeout / 1000;
147  timeout.tv_usec = (nTimeout % 1000) * 1000;
148  return timeout;
149 }
150 
151 struct timeval MillisToTimeval(std::chrono::milliseconds ms)
152 {
154 }
int ret
#define Assert(val)
Identity function.
Definition: check.h:73
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:70
std::chrono::time_point< NodeClock > time_point
Definition: time.h:19
static std::atomic< int64_t > nMockTime(0)
For testing.
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:143
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:105
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:100
int64_t GetTime()
Definition: time.cpp:115
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:130
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:89
static T GetSystemTime()
Definition: time.cpp:82
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
Definition: time.cpp:27
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:117
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:55
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1165
assert(!tx.IsCoinBase())