]>
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 | ||
09864abc | 12 | #include <chrono> |
ad49c256 WL |
13 | #include <boost/date_time/posix_time/posix_time.hpp> |
14 | #include <boost/thread.hpp> | |
15 | ||
16 | using namespace std; | |
17 | ||
c63a73d1 | 18 | static int64_t nMockTime = 0; //! For unit testing |
ad49c256 WL |
19 | |
20 | int64_t GetTime() | |
21 | { | |
22 | if (nMockTime) return nMockTime; | |
23 | ||
24 | return time(NULL); | |
25 | } | |
26 | ||
27 | void SetMockTime(int64_t nMockTimeIn) | |
28 | { | |
29 | nMockTime = nMockTimeIn; | |
30 | } | |
31 | ||
32 | int64_t GetTimeMillis() | |
33 | { | |
09864abc JG |
34 | return std::chrono::duration_cast<std::chrono::milliseconds>( |
35 | std::chrono::system_clock::now().time_since_epoch()).count(); | |
ad49c256 WL |
36 | } |
37 | ||
38 | int64_t GetTimeMicros() | |
39 | { | |
09864abc JG |
40 | return std::chrono::duration_cast<std::chrono::microseconds>( |
41 | std::chrono::system_clock::now().time_since_epoch()).count(); | |
ad49c256 WL |
42 | } |
43 | ||
44 | void MilliSleep(int64_t n) | |
45 | { | |
c63a73d1 MF |
46 | |
47 | /** | |
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 | |
51 | */ | |
ad49c256 WL |
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)); | |
56 | #else | |
57 | //should never get here | |
58 | #error missing boost sleep implementation | |
59 | #endif | |
60 | } | |
61 | ||
62 | std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime) | |
63 | { | |
64 | // std::locale takes ownership of the pointer | |
65 | std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat)); | |
66 | std::stringstream ss; | |
67 | ss.imbue(loc); | |
68 | ss << boost::posix_time::from_time_t(nTime); | |
69 | return ss.str(); | |
70 | } |