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 GetTime() {
115  return GetTime<std::chrono::seconds>().count();
116 }
117 
118 std::string FormatISO8601DateTime(int64_t nTime) {
119  struct tm ts;
120  time_t time_val = nTime;
121 #ifdef _WIN32
122  if (gmtime_s(&ts, &time_val) != 0) {
123 #else
124  if (gmtime_r(&time_val, &ts) == nullptr) {
125 #endif
126  return {};
127  }
128  return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900,
129  ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min,
130  ts.tm_sec);
131 }
132 
133 std::string FormatISO8601Date(int64_t nTime) {
134  struct tm ts;
135  time_t time_val = nTime;
136 #ifdef _WIN32
137  if (gmtime_s(&ts, &time_val) != 0) {
138 #else
139  if (gmtime_r(&time_val, &ts) == nullptr) {
140 #endif
141  return {};
142  }
143  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1,
144  ts.tm_mday);
145 }
146 
147 int64_t ParseISO8601DateTime(const std::string &str) {
148  static const boost::posix_time::ptime epoch =
149  boost::posix_time::from_time_t(0);
150  static const std::locale loc(
151  std::locale::classic(),
152  new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
153  std::istringstream iss(str);
154  iss.imbue(loc);
155  boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
156  iss >> ptime;
157  if (ptime.is_not_a_date_time() || epoch > ptime) {
158  return 0;
159  }
160  return (ptime - epoch).total_seconds();
161 }
162 
163 struct timeval MillisToTimeval(int64_t nTimeout) {
164  struct timeval timeout;
165  timeout.tv_sec = nTimeout / 1000;
166  timeout.tv_usec = (nTimeout % 1000) * 1000;
167  return timeout;
168 }
169 
170 struct timeval MillisToTimeval(std::chrono::milliseconds ms) {
172 }
#define Assert(val)
Identity function.
Definition: check.h:84
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:163
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
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:133
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:147
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:118
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:45
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
assert(!tx.IsCoinBase())