PIVX Core  5.6.99
P2P Digital Currency
utiltime.cpp
Go to the documentation of this file.
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 void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
23 
24 static std::atomic<int64_t> nMockTime(0);
25 
26 
27 int64_t GetTime()
28 {
29  int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
30  if (mocktime) return mocktime;
31 
32  time_t now = time(nullptr);
33  assert(now > 0);
34  return now;
35 }
36 
37 template <typename T>
38 T GetTime()
39 {
40  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
41 
42  return std::chrono::duration_cast<T>(
43  mocktime.count() ?
44  mocktime :
45  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 void SetMockTime(int64_t nMockTimeIn)
52 {
53  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
54 }
55 
56 int64_t GetMockTime()
57 {
58  return nMockTime.load(std::memory_order_relaxed);
59 }
60 
61 int64_t GetTimeMillis()
62 {
63  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
64  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
65  assert(now > 0);
66  return now;
67 }
68 
70 {
71  return GetTimeMicros()/1000000;
72 }
73 
74 int64_t GetTimeMicros()
75 {
76  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
77  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
78  assert(now > 0);
79  return now;
80 }
81 
82 void MilliSleep(int64_t n)
83 {
84  boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
85 }
86 
87 std::string DurationToDHMS(int64_t nDurationTime)
88 {
89  int seconds = nDurationTime % 60;
90  nDurationTime /= 60;
91  int minutes = nDurationTime % 60;
92  nDurationTime /= 60;
93  int hours = nDurationTime % 24;
94  int days = nDurationTime / 24;
95  if (days)
96  return strprintf("%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
97  if (hours)
98  return strprintf("%02dh:%02dm:%02ds", hours, minutes, seconds);
99  return strprintf("%02dm:%02ds", minutes, seconds);
100 }
101 
102 std::string FormatISO8601DateTime(int64_t nTime) {
103  struct tm ts;
104  time_t time_val = nTime;
105 #ifdef HAVE_GMTIME_R
106  if (gmtime_r(&time_val, &ts) == nullptr) {
107 #else
108  if (gmtime_s(&ts, &time_val) != 0) {
109 #endif
110  return {};
111  }
112  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 std::string FormatISO8601DateTimeForBackup(int64_t nTime) {
116  struct tm ts;
117  time_t time_val = nTime;
118 #ifdef HAVE_GMTIME_R
119  if (gmtime_r(&time_val, &ts) == nullptr) {
120 #else
121  if (gmtime_s(&ts, &time_val) != 0) {
122 #endif
123  return {};
124  }
125  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 std::string FormatISO8601Date(int64_t nTime) {
129  struct tm ts;
130  time_t time_val = nTime;
131 #ifdef HAVE_GMTIME_R
132  if (gmtime_r(&time_val, &ts) == nullptr) {
133 #else
134  if (gmtime_s(&ts, &time_val) != 0) {
135 #endif
136  return {};
137  }
138  return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
139 }
140 
141 std::string FormatISO8601Time(int64_t nTime) {
142  struct tm ts;
143  time_t time_val = nTime;
144 #ifdef HAVE_GMTIME_R
145  if (gmtime_r(&time_val, &ts) == nullptr) {
146 #else
147  if (gmtime_s(&ts, &time_val) != 0) {
148 #endif
149  return {};
150  }
151  return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
152 }
#define T(expected, seed, data)
#define strprintf
Definition: tinyformat.h:1056
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: utiltime.cpp:74
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: utiltime.cpp:61
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: utiltime.cpp:69
void UninterruptibleSleep(const std::chrono::microseconds &n)
Definition: utiltime.cpp:22
std::string FormatISO8601Date(int64_t nTime)
Definition: utiltime.cpp:128
std::string DurationToDHMS(int64_t nDurationTime)
Definition: utiltime.cpp:87
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: utiltime.cpp:51
std::string FormatISO8601Time(int64_t nTime)
Definition: utiltime.cpp:141
int64_t GetMockTime()
For testing.
Definition: utiltime.cpp:56
std::string FormatISO8601DateTimeForBackup(int64_t nTime)
Definition: utiltime.cpp:115
void MilliSleep(int64_t n)
Definition: utiltime.cpp:82
int64_t GetTime()
DEPRECATED Use either GetSystemTimeInSeconds (not mockable) or GetTime<T> (mockable)
Definition: utiltime.cpp:27
std::string FormatISO8601DateTime(int64_t nTime)
ISO 8601 formatting is preferred.
Definition: utiltime.cpp:102