Bitcoin Core  27.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 void SetMockTime(int64_t nMockTimeIn)
82 {
83  Assert(nMockTimeIn >= 0);
84  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
85 }
86 
87 void SetMockTime(std::chrono::seconds mock_time_in)
88 {
89  nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
90 }
91 
92 std::chrono::seconds GetMockTime()
93 {
94  return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
95 }
96 
97 int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
98 
99 std::string FormatISO8601DateTime(int64_t nTime) {
100  struct tm ts;
101  time_t time_val = nTime;
102 #ifdef HAVE_GMTIME_R
103  if (gmtime_r(&time_val, &ts) == nullptr) {
104 #else
105  if (gmtime_s(&ts, &time_val) != 0) {
106 #endif
107  return {};
108  }
109  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);
110 }
111 
112 std::string FormatISO8601Date(int64_t nTime) {
113  struct tm ts;
114  time_t time_val = nTime;
115 #ifdef HAVE_GMTIME_R
116  if (gmtime_r(&time_val, &ts) == nullptr) {
117 #else
118  if (gmtime_s(&ts, &time_val) != 0) {
119 #endif
120  return {};
121  }
122  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
123 }
124 
125 struct timeval MillisToTimeval(int64_t nTimeout)
126 {
127  struct timeval timeout;
128  timeout.tv_sec = nTimeout / 1000;
129  timeout.tv_usec = (nTimeout % 1000) * 1000;
130  return timeout;
131 }
132 
133 struct timeval MillisToTimeval(std::chrono::milliseconds ms)
134 {
136 }
int ret
#define Assert(val)
Identity function.
Definition: check.h:77
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:17
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:125
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:23
std::chrono::seconds GetMockTime()
For testing.
Definition: time.cpp:92
int64_t GetTime()
Definition: time.cpp:97
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:112
void SetMockTime(int64_t nMockTimeIn)
DEPRECATED Use SetMockTime with chrono type.
Definition: time.cpp:81
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:99
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:1162
assert(!tx.IsCoinBase())