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