Dogecoin Core  1.14.2
P2P Digital Currency
utiltime.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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/bitcoin-config.h"
8 #endif
9 
10 #include "utiltime.h"
11 
12 #include <boost/date_time/posix_time/posix_time.hpp>
13 #include <boost/thread.hpp>
14 
15 using namespace std;
16 
17 static int64_t nMockTime = 0;
18 
19 int64_t GetTime()
20 {
21  if (nMockTime) return nMockTime;
22 
23  time_t now = time(NULL);
24  assert(now > 0);
25  return now;
26 }
27 
28 void SetMockTime(int64_t nMockTimeIn)
29 {
30  nMockTime = nMockTimeIn;
31 }
32 
33 int64_t GetTimeMillis()
34 {
35  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
36  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
37  assert(now > 0);
38  return now;
39 }
40 
41 int64_t GetTimeMicros()
42 {
43  int64_t now = (boost::posix_time::microsec_clock::universal_time() -
44  boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
45  assert(now > 0);
46  return now;
47 }
48 
50 {
51  return GetTimeMicros()/1000000;
52 }
53 
56 {
57  if (nMockTime) return nMockTime*1000000;
58 
59  return GetTimeMicros();
60 }
61 
62 void MilliSleep(int64_t n)
63 {
64 
70 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
71  boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
72 #elif defined(HAVE_WORKING_BOOST_SLEEP)
73  boost::this_thread::sleep(boost::posix_time::milliseconds(n));
74 #else
75 //should never get here
76 #error missing boost sleep implementation
77 #endif
78 }
79 
80 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
81 {
82  static std::locale classic(std::locale::classic());
83  // std::locale takes ownership of the pointer
84  std::locale loc(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 }
int64_t GetTimeMicros()
Definition: utiltime.cpp:41
int64_t GetTimeMillis()
Definition: utiltime.cpp:33
int64_t GetSystemTimeInSeconds()
Definition: utiltime.cpp:49
int64_t GetTime()
GetTimeMicros() and GetTimeMillis() both return the system time, but in different units.
Definition: utiltime.cpp:19
int64_t GetLogTimeMicros()
Return a time useful for the debug log.
Definition: utiltime.cpp:55
std::string DateTimeStrFormat(const char *pszFormat, int64_t nTime)
Definition: utiltime.cpp:80
void SetMockTime(int64_t nMockTimeIn)
Definition: utiltime.cpp:28
void MilliSleep(int64_t n)
Definition: utiltime.cpp:62