Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
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 <util/time.h>
11 
12 #include <util/check.h>
13 
14 #include <atomic>
15 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include <ctime>
17 #include <thread>
18 
19 #include <tinyformat.h>
20 
21 void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
22 
23 static std::atomic<int64_t> nMockTime(0);
24 
25 int64_t GetTime()
26 {
27  int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
28  if (mocktime) return mocktime;
29 
30  time_t now = time(nullptr);
31  assert(now > 0);
32  return now;
33 }
34 
35 template <typename T>
36 T GetTime()
37 {
38  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
39 
40  return std::chrono::duration_cast<T>(
41  mocktime.count() ?
42  mocktime :
43  std::chrono::microseconds{GetTimeMicros()});
44 }
45 template std::chrono::seconds GetTime();
46 template std::chrono::milliseconds GetTime();
47 template std::chrono::microseconds GetTime();
48 
49 void SetMockTime(int64_t nMockTimeIn)
50 {
51  Assert(nMockTimeIn >= 0);
52  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
53 }
54 
55 int64_t GetMockTime()
56 {
57  return nMockTime.load(std::memory_order_relaxed);
58 }
59 
60 int64_t GetTimeMillis()
61 {
62  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
63  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
64  assert(now > 0);
65  return now;
66 }
67 
68 int64_t GetTimeMicros()
69 {
70  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
71  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
72  assert(now > 0);
73  return now;
74 }
75 
77 {
78  return GetTimeMicros()/1000000;
79 }
80 
81 std::string FormatISO8601DateTime(int64_t nTime) {
82  struct tm ts;
83  time_t time_val = nTime;
84 #ifdef HAVE_GMTIME_R
85  if (gmtime_r(&time_val, &ts) == nullptr) {
86 #else
87  if (gmtime_s(&ts, &time_val) != 0) {
88 #endif
89  return {};
90  }
91  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);
92 }
93 
94 std::string FormatISO8601Date(int64_t nTime) {
95  struct tm ts;
96  time_t time_val = nTime;
97 #ifdef HAVE_GMTIME_R
98  if (gmtime_r(&time_val, &ts) == nullptr) {
99 #else
100  if (gmtime_s(&ts, &time_val) != 0) {
101 #endif
102  return {};
103  }
104  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
105 }
106 
107 int64_t ParseISO8601DateTime(const std::string& str)
108 {
109  static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
110  static const std::locale loc(std::locale::classic(),
111  new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
112  std::istringstream iss(str);
113  iss.imbue(loc);
114  boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
115  iss >> ptime;
116  if (ptime.is_not_a_date_time() || epoch > ptime)
117  return 0;
118  return (ptime - epoch).total_seconds();
119 }
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:94
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:60
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:81
static std::atomic< int64_t > nMockTime(0)
For testing.
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: time.cpp:76
int64_t ParseISO8601DateTime(const std::string &str)
Definition: time.cpp:107
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: time.cpp:49
int64_t GetMockTime()
For testing.
Definition: time.cpp:55
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:68
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: time.cpp:21
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:25
#define Assert(val)
Identity function.
Definition: check.h:57