PRCYCoin  2.0.0.7rc1
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 // 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)
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 
17 static int64_t nMockTime = 0;
18 
19 int64_t GetTime()
20 {
21  if (nMockTime) return nMockTime;
22 
23  return time(NULL);
24 }
25 
26 void SetMockTime(int64_t nMockTimeIn)
27 {
28  nMockTime = nMockTimeIn;
29 }
30 
31 int64_t GetTimeMillis()
32 {
33  return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
34  boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1)))
35  .total_milliseconds();
36 }
37 
38 int64_t GetTimeMicros()
39 {
40  return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) -
41  boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1)))
42  .total_microseconds();
43 }
44 
45 void MilliSleep(int64_t n)
46 {
47  boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
48 }
49 
50 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
51 {
52  // std::locale takes ownership of the pointer
53  std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
54  std::stringstream ss;
55  ss.imbue(loc);
56  ss << boost::posix_time::from_time_t(nTime);
57  return ss.str();
58 }
59 
60 std::string DurationToDHMS(int64_t nDurationTime)
61 {
62  int seconds = nDurationTime % 60;
63  nDurationTime /= 60;
64  int minutes = nDurationTime % 60;
65  nDurationTime /= 60;
66  int hours = nDurationTime % 24;
67  int days = nDurationTime / 24;
68  if (days)
69  return strprintf("%dd %02dh:%02dm:%02ds", days, hours, minutes, seconds);
70  if (hours)
71  return strprintf("%02dh:%02dm:%02ds", hours, minutes, seconds);
72  return strprintf("%02dm:%02ds", minutes, seconds);
73 }
utiltime.h
GetTime
int64_t GetTime()
For unit testing.
Definition: utiltime.cpp:19
SetMockTime
void SetMockTime(int64_t nMockTimeIn)
Definition: utiltime.cpp:26
DateTimeStrFormat
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:50
tinyformat.h
prcycoin-config.h
GetTimeMillis
int64_t GetTimeMillis()
Definition: utiltime.cpp:31
strprintf
#define strprintf
Definition: tinyformat.h:1056
GetTimeMicros
int64_t GetTimeMicros()
Definition: utiltime.cpp:38
MilliSleep
void MilliSleep(int64_t n)
Definition: utiltime.cpp:45
DurationToDHMS
std::string DurationToDHMS(int64_t nDurationTime)
Definition: utiltime.cpp:60