]>
Commit | Line | Data |
---|---|---|
ad49c256 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
c63a73d1 | 3 | // Distributed under the MIT software license, see the accompanying |
ad49c256 WL |
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 | ||
c63a73d1 | 17 | static int64_t nMockTime = 0; //! For unit testing |
ad49c256 WL |
18 | |
19 | int64_t GetTime() | |
20 | { | |
21 | if (nMockTime) return nMockTime; | |
22 | ||
23 | return time(NULL); | |
24 | } | |
25 | ||
26 | void SetMockTime(int64_t nMockTimeIn) | |
27 | { | |
28 | nMockTime = nMockTimeIn; | |
29 | } | |
30 | ||
31 | int64_t GetTimeMillis() | |
32 | { | |
33 | return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - | |
34 | boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds(); | |
35 | } | |
36 | ||
37 | int64_t GetTimeMicros() | |
38 | { | |
39 | return (boost::posix_time::ptime(boost::posix_time::microsec_clock::universal_time()) - | |
40 | boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds(); | |
41 | } | |
42 | ||
43 | void MilliSleep(int64_t n) | |
44 | { | |
c63a73d1 MF |
45 | |
46 | /** | |
47 | * Boost's sleep_for was uninterruptable when backed by nanosleep from 1.50 | |
48 | * until fixed in 1.52. Use the deprecated sleep method for the broken case. | |
49 | * See: https://svn.boost.org/trac/boost/ticket/7238 | |
50 | */ | |
ad49c256 WL |
51 | #if defined(HAVE_WORKING_BOOST_SLEEP_FOR) |
52 | boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); | |
53 | #elif defined(HAVE_WORKING_BOOST_SLEEP) | |
54 | boost::this_thread::sleep(boost::posix_time::milliseconds(n)); | |
55 | #else | |
56 | //should never get here | |
57 | #error missing boost sleep implementation | |
58 | #endif | |
59 | } | |
60 | ||
61 | std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime) | |
62 | { | |
63 | // std::locale takes ownership of the pointer | |
64 | std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat)); | |
65 | std::stringstream ss; | |
66 | ss.imbue(loc); | |
67 | ss << boost::posix_time::from_time_t(nTime); | |
68 | return ss.str(); | |
69 | } |