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)
7 #include <config/bitcoin-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 
UninterruptibleSleep(const std::chrono::microseconds & n)19 void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
20 
21 static std::atomic<int64_t> nMockTime(0); //!< For unit testing
22 
GetTime()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>
GetTime()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 
SetMockTime(int64_t nMockTimeIn)47 void SetMockTime(int64_t nMockTimeIn)
48 {
49     nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
50 }
51 
GetMockTime()52 int64_t GetMockTime()
53 {
54     return nMockTime.load(std::memory_order_relaxed);
55 }
56 
GetTimeMillis()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 
GetTimeMicros()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 
GetSystemTimeInSeconds()73 int64_t GetSystemTimeInSeconds()
74 {
75     return GetTimeMicros()/1000000;
76 }
77 
FormatISO8601DateTime(int64_t nTime)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 }
117