Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

utiltime.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/dash-config.h>
8 #endif
9 
10 #include <tinyformat.h>
11 #include <utiltime.h>
12 
13 #include <atomic>
14 
15 #include <boost/date_time/posix_time/posix_time.hpp>
16 #include <boost/thread.hpp>
17 
18 #include <chrono>
19 
20 static std::atomic<int64_t> nMockTime(0);
21 
22 int64_t GetTime()
23 {
24  int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
25  if (mocktime) return mocktime;
26 
27  time_t now = time(nullptr);
28  assert(now > 0);
29  return now;
30 }
31 
32 template <typename T>
33 T GetTime()
34 {
35  const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
36 
37  return std::chrono::duration_cast<T>(
38  mocktime.count() ?
39  mocktime :
40  std::chrono::microseconds{GetTimeMicros()});
41 }
42 template std::chrono::seconds GetTime();
43 template std::chrono::milliseconds GetTime();
44 template std::chrono::microseconds GetTime();
45 
46 void SetMockTime(int64_t nMockTimeIn)
47 {
48  nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
49 }
50 
51 int64_t GetMockTime()
52 {
53  return nMockTime.load(std::memory_order_relaxed);
54 }
55 
56 int64_t GetTimeMillis()
57 {
58  int64_t now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
59  assert(now > 0);
60  return now;
61 }
62 
63 int64_t GetTimeMicros()
64 {
65  int64_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
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 DateTimeStrFormat(const char* pszFormat, int64_t nTime)
94 {
95  static std::locale classic(std::locale::classic());
96  // std::locale takes ownership of the pointer
97  std::locale loc(classic, new boost::posix_time::time_facet(pszFormat));
98  std::stringstream ss;
99  ss.imbue(loc);
100  ss << boost::posix_time::from_time_t(nTime);
101  return ss.str();
102 }
void MilliSleep(int64_t n)
Definition: utiltime.cpp:75
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:93
void SetMockTime(int64_t nMockTimeIn)
For testing.
Definition: utiltime.cpp:46
int64_t GetTimeMicros()
Returns the system time (not mockable)
Definition: utiltime.cpp:63
int64_t GetTime()
Return system time (or mocked time, if set)
Definition: utiltime.cpp:22
int64_t GetSystemTimeInSeconds()
Returns the system time (not mockable)
Definition: utiltime.cpp:70
static std::atomic< int64_t > nMockTime(0)
For unit testing.
int64_t GetTimeMillis()
Returns the system time (not mockable)
Definition: utiltime.cpp:56
int64_t GetMockTime()
For testing.
Definition: utiltime.cpp:51
Released under the MIT license