]> Git Repo - VerusCoin.git/blob - src/utiltime.cpp
Auto merge of #2767 - syd0:cleanup-reserved-include-guards, r=str4d
[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     boost::this_thread::sleep_for(boost::chrono::milliseconds(n));
47 }
48
49 std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
50 {
51     // std::locale takes ownership of the pointer
52     std::locale loc(std::locale::classic(), new boost::posix_time::time_facet(pszFormat));
53     std::stringstream ss;
54     ss.imbue(loc);
55     ss << boost::posix_time::from_time_t(nTime);
56     return ss.str();
57 }
This page took 0.028767 seconds and 4 git commands to generate.