Changeset View
Changeset View
Standalone View
Standalone View
src/utiltime.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include "config/bitcoin-config.h" | #include "config/bitcoin-config.h" | ||||
#endif | #endif | ||||
#include "utiltime.h" | #include "utiltime.h" | ||||
#include <atomic> | |||||
#include <boost/date_time/posix_time/posix_time.hpp> | #include <boost/date_time/posix_time/posix_time.hpp> | ||||
#include <boost/thread.hpp> | #include <boost/thread.hpp> | ||||
//!< For unit testing | //!< For unit testing | ||||
static int64_t nMockTime = 0; | static std::atomic<int64_t> nMockTime(0); | ||||
int64_t GetTime() { | int64_t GetTime() { | ||||
if (nMockTime) return nMockTime; | int64_t mocktime = nMockTime.load(std::memory_order_relaxed); | ||||
if (mocktime) { | |||||
return mocktime; | |||||
} | |||||
time_t now = time(nullptr); | time_t now = time(nullptr); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
void SetMockTime(int64_t nMockTimeIn) { | void SetMockTime(int64_t nMockTimeIn) { | ||||
nMockTime = nMockTimeIn; | nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetTimeMillis() { | int64_t GetTimeMillis() { | ||||
int64_t now = (boost::posix_time::microsec_clock::universal_time() - | int64_t now = (boost::posix_time::microsec_clock::universal_time() - | ||||
boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | ||||
.total_milliseconds(); | .total_milliseconds(); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
int64_t GetTimeMicros() { | int64_t GetTimeMicros() { | ||||
int64_t now = (boost::posix_time::microsec_clock::universal_time() - | int64_t now = (boost::posix_time::microsec_clock::universal_time() - | ||||
boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | boost::posix_time::ptime(boost::gregorian::date(1970, 1, 1))) | ||||
.total_microseconds(); | .total_microseconds(); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
int64_t GetSystemTimeInSeconds() { | int64_t GetSystemTimeInSeconds() { | ||||
return GetTimeMicros() / 1000000; | return GetTimeMicros() / 1000000; | ||||
} | } | ||||
/** Return a time useful for the debug log */ | /** Return a time useful for the debug log */ | ||||
int64_t GetLogTimeMicros() { | int64_t GetLogTimeMicros() { | ||||
if (nMockTime) return nMockTime * 1000000; | int64_t mocktime = nMockTime.load(std::memory_order_relaxed); | ||||
if (mocktime) { | |||||
return mocktime * 1000000; | |||||
} | |||||
return GetTimeMicros(); | return GetTimeMicros(); | ||||
} | } | ||||
void MilliSleep(int64_t n) { | void MilliSleep(int64_t n) { | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); | boost::this_thread::sleep_for(boost::chrono::milliseconds(n)); | ||||
} | } | ||||
Show All 9 Lines |