1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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"
13 #include <boost/date_time/posix_time/posix_time.hpp>
14 #include <boost/thread.hpp>
18 static int64_t nMockTime = 0; //! For unit testing
22 if (nMockTime) return nMockTime;
27 void SetMockTime(int64_t nMockTimeIn)
29 nMockTime = nMockTimeIn;
32 int64_t GetTimeMillis()
34 return std::chrono::duration_cast<std::chrono::milliseconds>(
35 std::chrono::system_clock::now().time_since_epoch()).count();
38 int64_t GetTimeMicros()
40 return std::chrono::duration_cast<std::chrono::microseconds>(
41 std::chrono::system_clock::now().time_since_epoch()).count();
44 void MilliSleep(int64_t n)
48 * Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50
49 * until fixed in 1.52. Use the deprecated sleep method for the broken case.
50 * See: https://svn.boost.org/trac/boost/ticket/7238
52 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
53 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
54 #elif defined(HAVE_WORKING_BOOST_SLEEP)
55 boost::this_thread::sleep(boost::posix_time::milliseconds(n));
57 //should never get here
58 #error missing boost sleep implementation
62 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
64 // std::locale takes ownership of the pointer
65 std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
68 ss << boost::posix_time::from_time_t(nTime);