]> Git Repo - VerusCoin.git/blob - src/utiltime.cpp
Make some global variables less-global (static)
[VerusCoin.git] / src / utiltime.cpp
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.
5
6 #if defined(HAVE_CONFIG_H)
7 #include "config/bitcoin-config.h"
8 #endif
9
10 #include "utiltime.h"
11
12 #include <chrono>
13 #include <boost/date_time/posix_time/posix_time.hpp>
14 #include <boost/thread.hpp>
15
16 using namespace std;
17
18 static int64_t nMockTime = 0;  //! For unit testing
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 {
34     return std::chrono::duration_cast<std::chrono::milliseconds>(
35             std::chrono::system_clock::now().time_since_epoch()).count();
36 }
37
38 int64_t GetTimeMicros()
39 {
40     return std::chrono::duration_cast<std::chrono::microseconds>(
41             std::chrono::system_clock::now().time_since_epoch()).count();
42 }
43
44 void MilliSleep(int64_t n)
45 {
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  */
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 }
This page took 0.027284 seconds and 4 git commands to generate.