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