Dash Core  0.12.2.1
P2P Digital Currency
utiltime.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 <boost/date_time/posix_time/posix_time.hpp>
14 #include <boost/thread.hpp>
15 
16 using namespace std;
17 
18 static int64_t nMockTime = 0;
19 
20 int64_t GetTime()
21 {
22  if (nMockTime) return nMockTime;
23 
24  time_t now = time(NULL);
25  assert(now > 0);
26  return now;
27 }
28 
29 void SetMockTime(int64_t nMockTimeIn)
30 {
31  nMockTime = nMockTimeIn;
32 }
33 
34 int64_t GetTimeMillis()
35 {
36  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
37  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
38  assert(now > 0);
39  return now;
40 }
41 
42 int64_t GetTimeMicros()
43 {
44  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
45  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
46  assert(now > 0);
47  return now;
48 }
49 
51 {
52  return GetTimeMicros()/1000000;
53 }
54 
57 {
58  if (nMockTime) return nMockTime*1000000;
59 
60  return GetTimeMicros();
61 }
62 
63 void MilliSleep(int64_t n)
64 {
65 
71 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
72  boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
73 #elif defined(HAVE_WORKING_BOOST_SLEEP)
74  boost::this_thread::sleep(boost::posix_time::milliseconds(n));
75 #else
76 //should never get here
77 #error missing boost sleep implementation
78 #endif
79 }
80 
81 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
82 {
83  // std::locale takes ownership of the pointer
84  std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
85  std::stringstream ss;
86  ss.imbue(loc);
87  ss << boost::posix_time::from_time_t(nTime);
88  return ss.str();
89 }
90 
91 std::string DurationToDHMS(int64_t nDurationTime)
92 {
93  int seconds = nDurationTime % 60;
94  nDurationTime /= 60;
95  int minutes = nDurationTime % 60;
96  nDurationTime /= 60;
97  int hours = nDurationTime % 24;
98  int days = nDurationTime / 24;
99  if(days)
100  return strprintf("%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
101  if(hours)
102  return strprintf("%02dh:%02dm:%02ds", hours, minutes, seconds);
103  return strprintf("%02dm:%02ds", minutes, seconds);
104 }
std::string DurationToDHMS(int64_t nDurationTime)
Definition: utiltime.cpp:91
int64_t GetLogTimeMicros()
Definition: utiltime.cpp:56
void MilliSleep(int64_t n)
Definition: utiltime.cpp:63
#define strprintf
Definition: tinyformat.h:1011
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:81
void SetMockTime(int64_t nMockTimeIn)
Definition: utiltime.cpp:29
int64_t GetTimeMicros()
Definition: utiltime.cpp:42
static int64_t nMockTime
Definition: utiltime.cpp:18
int64_t GetSystemTimeInSeconds()
Definition: utiltime.cpp:50
int64_t GetTimeMillis()
Definition: utiltime.cpp:34
int64_t GetTime()
For unit testing.
Definition: utiltime.cpp:20