[gitian] Use vm-builder_0.12.4+bzr494 on Debian
[bitcoinplatinum.git] / src / utiltime.cpp
blob3202c47f1dc92ce9bc596957fe0d020af0c5b4da
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"
8 #endif
10 #include "utiltime.h"
12 #include <boost/date_time/posix_time/posix_time.hpp>
13 #include <boost/thread.hpp>
15 using namespace std;
17 static int64_t nMockTime = 0; //! For unit testing
19 int64_t GetTime()
21 if (nMockTime) return nMockTime;
23 return time(NULL);
26 void SetMockTime(int64_t nMockTimeIn)
28 nMockTime = nMockTimeIn;
31 int64_t GetTimeMillis()
33 return (boost::posix_time::microsec_clock::universal_time() -
34 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
37 int64_t GetTimeMicros()
39 return (boost::posix_time::microsec_clock::universal_time() -
40 boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
43 /** Return a time useful for the debug log */
44 int64_t GetLogTimeMicros()
46 if (nMockTime) return nMockTime*1000000;
48 return GetTimeMicros();
51 void MilliSleep(int64_t n)
54 /**
55 * Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50
56 * until fixed in 1.52. Use the deprecated sleep method for the broken case.
57 * See: https://svn.boost.org/trac/boost/ticket/7238
59 #if defined(HAVE_WORKING_BOOST_SLEEP_FOR)
60 boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
61 #elif defined(HAVE_WORKING_BOOST_SLEEP)
62 boost::this_thread::sleep(boost::posix_time::milliseconds(n));
63 #else
64 //should never get here
65 #error missing boost sleep implementation
66 #endif
69 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
71 // std::locale takes ownership of the pointer
72 std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
73 std::stringstream ss;
74 ss.imbue(loc);
75 ss << boost::posix_time::from_time_t(nTime);
76 return ss.str();