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