Bitcoin Core  0.18.99
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)
8 #endif
9 
10 #include <util/time.h>
11 
12 #include <atomic>
13 #include <boost/date_time/posix_time/posix_time.hpp>
14 #include <boost/thread.hpp>
15 #include <ctime>
16 #include <tinyformat.h>
17 
18 static std::atomic<int64_t> nMockTime(0);
19 
20 int64_t GetTime()
21 {
22  int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
23  if (mocktime) return mocktime;
24 
25  time_t now = time(nullptr);
26  assert(now > 0);
27  return now;
28 }
29 
30 template <typename T>
31 T GetTime()
32 {
33  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
34 
35  return std::chrono::duration_cast<T>(
36  mocktime.count() ?
37  mocktime :
38  std::chrono::microseconds{GetTimeMicros()});
39 }
40 template std::chrono::seconds GetTime();
41 template std::chrono::milliseconds GetTime();
42 template std::chrono::microseconds GetTime();
43 
44 void SetMockTime(int64_t nMockTimeIn)
45 {
46  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
47 }
48 
49 int64_t GetMockTime()
50 {
51  return nMockTime.load(std::memory_order_relaxed);
52 }
53 
54 int64_t GetTimeMillis()
55 {
56  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
57  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
58  assert(now > 0);
59  return now;
60 }
61 
62 int64_t GetTimeMicros()
63 {
64  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
65  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
66  assert(now > 0);
67  return now;
68 }
69 
71 {
72  return GetTimeMicros()/1000000;
73 }
74 
75 void MilliSleep(int64_t n)
76 {
77 
83 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
84  boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
85 #elif defined(HAVE_WORKING_BOOST_SLEEP)
86  boost::this_thread::sleep(boost::posix_time::milliseconds(n));
87 #else
88 //should never get here
89 #error missing boost sleep implementation
90 #endif
91 }
92 
93 std::string FormatISO8601DateTime(int64_t nTime) {
94  struct tm ts;
95  time_t time_val = nTime;
96 #ifdef _MSC_VER
97  gmtime_s(&ts, &time_val);
98 #else
99  gmtime_r(&time_val, &ts);
100 #endif
101  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);
102 }
103 
104 std::string FormatISO8601Date(int64_t nTime) {
105  struct tm ts;
106  time_t time_val = nTime;
107 #ifdef _MSC_VER
108  gmtime_s(&ts, &time_val);
109 #else
110  gmtime_r(&time_val, &ts);
111 #endif
112  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
113 }
std::string FormatISO8601Date(int64_t nTime)
Definition: time.cpp:104
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: time.cpp:54
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1067
void MilliSleep(int64_t n)
Definition: time.cpp:75
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: time.cpp:93
static std::atomic< int64_t > nMockTime(0)
For unit testing.
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: time.cpp:70
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: time.cpp:44
int64_t GetMockTime()
For testing.
Definition: time.cpp:49
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: time.cpp:62
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: time.cpp:20