Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 The Bitcoin developers
3 : // Copyright (c) 2016-2021 The PIVX Core developers
4 : // Distributed under the MIT software license, see the accompanying
5 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 :
7 : #if defined(HAVE_CONFIG_H)
8 : #include "config/pivx-config.h"
9 : #endif
10 :
11 : #include "tinyformat.h"
12 : #include "utiltime.h"
13 :
14 : #include <atomic>
15 : #include <boost/date_time/posix_time/posix_time.hpp>
16 : #include <boost/thread.hpp>
17 : #include <ctime>
18 : #include <thread>
19 : #include <tinyformat.h>
20 :
21 :
22 1 : void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
23 :
24 : static std::atomic<int64_t> nMockTime(0); //!< For unit testing
25 :
26 :
27 8815292 : int64_t GetTime()
28 : {
29 8815292 : int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
30 8815292 : if (mocktime) return mocktime;
31 :
32 7971987 : time_t now = time(nullptr);
33 7971987 : assert(now > 0);
34 : return now;
35 : }
36 :
37 : template <typename T>
38 3954850 : T GetTime()
39 : {
40 3954850 : const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
41 :
42 7909690 : return std::chrono::duration_cast<T>(
43 3954850 : mocktime.count() ?
44 : mocktime :
45 3448162 : std::chrono::microseconds{GetTimeMicros()});
46 : }
47 : template std::chrono::seconds GetTime();
48 : template std::chrono::milliseconds GetTime();
49 : template std::chrono::microseconds GetTime();
50 :
51 10845 : void SetMockTime(int64_t nMockTimeIn)
52 : {
53 10845 : nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
54 10845 : }
55 :
56 4386592 : int64_t GetMockTime()
57 : {
58 4386592 : return nMockTime.load(std::memory_order_relaxed);
59 : }
60 :
61 1200722 : int64_t GetTimeMillis()
62 : {
63 2401444 : int64_t now = (boost::posix_time::microsec_clock::universal_time() -
64 2401444 : boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
65 1200722 : assert(now > 0);
66 1200722 : return now;
67 : }
68 :
69 8935958 : int64_t GetSystemTimeInSeconds()
70 : {
71 8935958 : return GetTimeMicros()/1000000;
72 : }
73 :
74 23855553 : int64_t GetTimeMicros()
75 : {
76 47711106 : int64_t now = (boost::posix_time::microsec_clock::universal_time() -
77 47711106 : boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
78 23855553 : assert(now > 0);
79 23855553 : return now;
80 : }
81 :
82 296373 : void MilliSleep(int64_t n)
83 : {
84 296373 : boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
85 296208 : }
86 :
87 0 : std::string DurationToDHMS(int64_t nDurationTime)
88 : {
89 0 : int seconds = nDurationTime % 60;
90 0 : nDurationTime /= 60;
91 0 : int minutes = nDurationTime % 60;
92 0 : nDurationTime /= 60;
93 0 : int hours = nDurationTime % 24;
94 0 : int days = nDurationTime / 24;
95 0 : if (days)
96 0 : return strprintf("%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
97 0 : if (hours)
98 0 : return strprintf("%02dh:%02dm:%02ds", hours, minutes, seconds);
99 0 : return strprintf("%02dm:%02ds", minutes, seconds);
100 : }
101 :
102 5021442 : std::string FormatISO8601DateTime(int64_t nTime) {
103 5021442 : struct tm ts;
104 5021442 : time_t time_val = nTime;
105 : #ifdef HAVE_GMTIME_R
106 5021442 : if (gmtime_r(&time_val, &ts) == nullptr) {
107 : #else
108 : if (gmtime_s(&ts, &time_val) != 0) {
109 : #endif
110 0 : return {};
111 : }
112 5021442 : 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);
113 : }
114 :
115 363 : std::string FormatISO8601DateTimeForBackup(int64_t nTime) {
116 363 : struct tm ts;
117 363 : time_t time_val = nTime;
118 : #ifdef HAVE_GMTIME_R
119 363 : if (gmtime_r(&time_val, &ts) == nullptr) {
120 : #else
121 : if (gmtime_s(&ts, &time_val) != 0) {
122 : #endif
123 0 : return {};
124 : }
125 363 : return strprintf(".%04i%02i%02iT%02i%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min);
126 : }
127 :
128 708 : std::string FormatISO8601Date(int64_t nTime) {
129 708 : struct tm ts;
130 708 : time_t time_val = nTime;
131 : #ifdef HAVE_GMTIME_R
132 708 : if (gmtime_r(&time_val, &ts) == nullptr) {
133 : #else
134 : if (gmtime_s(&ts, &time_val) != 0) {
135 : #endif
136 0 : return {};
137 : }
138 708 : return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
139 : }
140 :
141 71253 : std::string FormatISO8601Time(int64_t nTime) {
142 71253 : struct tm ts;
143 71253 : time_t time_val = nTime;
144 : #ifdef HAVE_GMTIME_R
145 71253 : if (gmtime_r(&time_val, &ts) == nullptr) {
146 : #else
147 : if (gmtime_s(&ts, &time_val) != 0) {
148 : #endif
149 0 : return {};
150 : }
151 71253 : return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
152 : }
|