Bitcoin Core  21.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-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 #if defined(HAVE_CONFIG_H)
8 #endif
9 
10 #include <compat.h>
11 #include <util/time.h>
12 
13 #include <util/check.h>
14 
15 #include <atomic>
16 #include <boost/date_time/posix_time/posix_time.hpp>
17 #include <ctime>
18 #include <thread>
19 
20 #include <tinyformat.h>
21 
22 void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
23 
24 static std::atomic<int64_t> nMockTime(0);
25 
26 int64_t GetTime()
27 {
28  int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
29  if (mocktime) return mocktime;
30 
31  time_t now = time(nullptr);
32  assert(now > 0);
33  return now;
34 }
35 
37 {
38  // std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
39  // to use the Unix epoch timestamp, prior to C++20, but in practice they almost
40  // certainly will. Any differing behavior will be assumed to be an error, unless
41  // certain platforms prove to consistently deviate, at which point we'll cope
42  // with it by adding offsets.
43 
44  // Create a new clock from time_t(0) and make sure that it represents 0
45  // seconds from the system_clock's time_since_epoch. Then convert that back
46  // to a time_t and verify that it's the same as before.
47  const time_t time_t_epoch{};
48  auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
49  if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
50  return false;
51  }
52 
53  time_t time_val = std::chrono::system_clock::to_time_t(clock);
54  if (time_val != time_t_epoch) {
55  return false;
56  }
57 
58  // Check that the above zero time is actually equal to the known unix timestamp.
59  struct tm epoch;
60 #ifdef HAVE_GMTIME_R
61  if (gmtime_r(&time_val, &epoch) == nullptr) {
62 #else
63  if (gmtime_s(&epoch, &time_val) != 0) {
64 #endif
65  return false;
66  }
67 
68  if ((epoch.tm_sec != 0) ||
69  (epoch.tm_min != 0) ||
70  (epoch.tm_hour != 0) ||
71  (epoch.tm_mday != 1) ||
72  (epoch.tm_mon != 0) ||
73  (epoch.tm_year != 70)) {
74  return false;
75  }
76  return true;
77 }
78 
79 template <typename T>
80 T GetTime()
81 {
82  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
83 
84  return std::chrono::duration_cast<T>(
85  mocktime.count() ?
86  mocktime :
87  std::chrono::microseconds{GetTimeMicros()});
88 }
89 template std::chrono::seconds GetTime();
90 template std::chrono::milliseconds GetTime();
91 template std::chrono::microseconds GetTime();
92 
93 template <typename T>
94 static T GetSystemTime()
95 {
96  const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
97  assert(now.count() > 0);
98  return now;
99 }
100 
101 void SetMockTime(int64_t nMockTimeIn)
102 {
103  Assert(nMockTimeIn >= 0);
104  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
105 }
106 
107 void SetMockTime(std::chrono::seconds mock_time_in)
108 {
109  nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
110 }
111 
112 std::chrono::seconds GetMockTime()
113 {
114  return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
115 }
116 
117 int64_t GetTimeMillis()
118 {
119  return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
120 }
121 
122 int64_t GetTimeMicros()
123 {
124  return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
125 }
126 
128 {
129  return int64_t{GetSystemTime<std::chrono::seconds>().count()};
130 }
131 
132 std::string FormatISO8601DateTime(int64_t nTime) {
133  struct tm ts;
134  time_t time_val = nTime;
135 #ifdef HAVE_GMTIME_R
136  if (gmtime_r(&time_val, &ts) == nullptr) {
137 #else
138  if (gmtime_s(&ts, &time_val) != 0) {
139 #endif
140  return {};
141  }
142  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);
143 }
144 
145 std::string FormatISO8601Date(int64_t nTime) {
146  struct tm ts;
147  time_t time_val = nTime;
148 #ifdef HAVE_GMTIME_R
149  if (gmtime_r(&time_val, &ts) == nullptr) {
150 #else
151  if (gmtime_s(&ts, &time_val) != 0) {
152 #endif
153  return {};
154  }
155  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
156 }
157 
158 int64_t ParseISO8601DateTime(const std::string& str)
159 {
160  static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
161  static const std::locale loc(std::locale::classic(),
162  new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
163  std::istringstream iss(str);
164  iss.imbue(loc);
165  boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
166  iss >> ptime;
167  if (ptime.is_not_a_date_time() || epoch > ptime)
168  return 0;
169  return (ptime - epoch).total_seconds();
170 }
171 
172 struct timeval MillisToTimeval(int64_t nTimeout)
173 {
174  struct timeval timeout;
175  timeout.tv_sec = nTimeout / 1000;
176  timeout.tv_usec = (nTimeout % 1000) * 1000;
177  return timeout;
178 }
179 
180 struct timeval MillisToTimeval(std::chrono::milliseconds ms)
181 {
183 }
FormatISO8601Date
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:145
check.h
ChronoSanityCheck
bool ChronoSanityCheck()
Sanity check epoch match normal Unix epoch.
Definition: time.cpp:36
GetMockTime
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:112
ParseISO8601DateTime
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:158
GetTime
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: time.cpp:26
bitcoin-config.h
FormatISO8601DateTime
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:132
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
tinyformat.h
nMockTime
static std::atomic< int64_t > nMockTime(0)
For testing.
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:101
compat.h
GetSystemTimeInSeconds
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: time.cpp:127
time.h
UninterruptibleSleep
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:22
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
MillisToTimeval
struct timeval MillisToTimeval(int64_t nTimeout)
Convert milliseconds to a struct timeval for e.g.
Definition: time.cpp:172
count_milliseconds
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:30
GetTimeMicros
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:122
assert
assert(std::addressof(::ChainstateActive().CoinsTip())==std::addressof(coins_cache))
GetTimeMillis
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:117
GetSystemTime
static T GetSystemTime()
Definition: time.cpp:94