Merge #9911: Wshadow: various gcc fixes
[bitcoinplatinum.git] / src / utiltime.cpp
bloba9936a645aa59f26bab10be5314628607adc0b4a
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.
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
10 #include "utiltime.h"
12 #include <boost/date_time/posix_time/posix_time.hpp>
13 #include <boost/thread.hpp>
15 static int64_t nMockTime = 0; //!< For unit testing
17 int64_t GetTime()
19 if (nMockTime) return nMockTime;
21 time_t now = time(NULL);
22 assert(now > 0);
23 return now;
26 void SetMockTime(int64_t nMockTimeIn)
28 nMockTime = nMockTimeIn;
31 int64_t GetTimeMillis()
33 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
34 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
35 assert(now > 0);
36 return now;
39 int64_t GetTimeMicros()
41 int64_t now = (boost::posix_time::microsec_clock::universal_time() -
42 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
43 assert(now > 0);
44 return now;
47 int64_t GetSystemTimeInSeconds()
49 return GetTimeMicros()/1000000;
52 /** Return a time useful for the debug log */
53 int64_t GetLogTimeMicros()
55 if (nMockTime) return nMockTime*1000000;
57 return GetTimeMicros();
60 void MilliSleep(int64_t n)
63 /**
64 * Boost's sleep_for was uninterruptible when backed by nanosleep from 1.50
65 * until fixed in 1.52. Use the deprecated sleep method for the broken case.
66 * See: https://svn.boost.org/trac/boost/ticket/7238
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
78 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
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();