Bitcoin ABC  0.29.4
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 
72  const std::chrono::seconds mocktime{
73  nMockTime.load(std::memory_order_relaxed)};
74 
75  const auto ret{mocktime.count()
76  ? mocktime
77  : std::chrono::system_clock::now().time_since_epoch()};
78  assert(ret > 0s);
79  return time_point{ret};
80 };
81 
82 template <typename T> static T GetSystemTime() {
83  const auto now = std::chrono::duration_cast<T>(
84  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  Assert(nMockTimeIn >= 0);
91  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
92 }
93 
94 void SetMockTime(std::chrono::seconds mock_time_in) {
95  nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
96 }
97 std::chrono::seconds GetMockTime() {
98  return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
99 }
100 
101 int64_t GetTimeMillis() {
102  return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
103 }
104 
105 int64_t GetTimeMicros() {
106  return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
107 }
108 
109 int64_t GetTime() {
110  return GetTime<std::chrono::seconds>().count();
111 }
112 
113 std::string FormatISO8601DateTime(int64_t nTime) {
114  struct tm ts;
115  time_t time_val = nTime;
116 #ifdef _WIN32
117  if (gmtime_s(&ts, &time_val) != 0) {
118 #else
119  if (gmtime_r(&time_val, &ts) == nullptr) {
120 #endif
121  return {};
122  }
123  return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900,
124  ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min,
125  ts.tm_sec);
126 }
127 
128 std::string FormatISO8601Date(int64_t nTime) {
129  struct tm ts;
130  time_t time_val = nTime;
131 #ifdef _WIN32
132  if (gmtime_s(&ts, &time_val) != 0) {
133 #else
134  if (gmtime_r(&time_val, &ts) == nullptr) {
135 #endif
136  return {};
137  }
138  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1,
139  ts.tm_mday);
140 }
141 
142 int64_t ParseISO8601DateTime(const std::string &str) {
143  static const boost::posix_time::ptime epoch =
144  boost::posix_time::from_time_t(0);
145  static const std::locale loc(
146  std::locale::classic(),
147  new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
148  std::istringstream iss(str);
149  iss.imbue(loc);
150  boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
151  iss >> ptime;
152  if (ptime.is_not_a_date_time() || epoch > ptime) {
153  return 0;
154  }
155  return (ptime - epoch).total_seconds();
156 }
157 
158 struct timeval MillisToTimeval(int64_t nTimeout) {
159  struct timeval timeout;
160  timeout.tv_sec = nTimeout / 1000;
161  timeout.tv_usec = (nTimeout % 1000) * 1000;
162  return timeout;
163 }
164 
165 struct timeval MillisToTimeval(std::chrono::milliseconds ms) {
167 }
#define Assert(val)
Identity function.
Definition: check.h:84
Implement std::hash so RCUPtr can be used as a key for maps or sets.
Definition: rcu.h:257
static time_point now() noexcept
Return current system time or mocked time, if set.
Definition: time.cpp:71
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:158
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:105
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:101
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:97
int64_t GetTime()
Definition: time.cpp:109
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:128
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:89
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:142
static T GetSystemTime()
Definition: time.cpp:82
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:113
constexpr int64_t count_milliseconds(std::chrono::milliseconds t)
Definition: time.h:58
#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())