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