Changeset View
Changeset View
Standalone View
Standalone View
src/util/time.cpp
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2019 The Bitcoin Core developers | // Copyright (c) 2009-2019 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. | ||||
#include <util/time.h> | |||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <compat.h> | #include <compat.h> | ||||
#include <util/time.h> | #include <util/check.h> | ||||
#include <boost/date_time/posix_time/posix_time.hpp> | #include <boost/date_time/posix_time/posix_time.hpp> | ||||
#include <tinyformat.h> | #include <tinyformat.h> | ||||
#include <atomic> | #include <atomic> | ||||
#include <ctime> | #include <ctime> | ||||
#include <thread> | #include <thread> | ||||
void UninterruptibleSleep(const std::chrono::microseconds &n) { | void UninterruptibleSleep(const std::chrono::microseconds &n) { | ||||
std::this_thread::sleep_for(n); | std::this_thread::sleep_for(n); | ||||
} | } | ||||
//! For unit testing | //! For testing | ||||
static std::atomic<int64_t> nMockTime(0); | static std::atomic<int64_t> nMockTime(0); | ||||
bool ChronoSanityCheck() { | bool ChronoSanityCheck() { | ||||
// std::chrono::system_clock.time_since_epoch and time_t(0) are not | // std::chrono::system_clock.time_since_epoch and time_t(0) are not | ||||
// guaranteed to use the Unix epoch timestamp, prior to C++20, but in | // guaranteed to use the Unix epoch timestamp, prior to C++20, but in | ||||
// practice they almost certainly will. Any differing behavior will be | // practice they almost certainly will. Any differing behavior will be | ||||
// assumed to be an error, unless certain platforms prove to consistently | // assumed to be an error, unless certain platforms prove to consistently | ||||
// deviate, at which point we'll cope with it by adding offsets. | // deviate, at which point we'll cope with it by adding offsets. | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
template <typename T> static T GetSystemTime() { | template <typename T> static T GetSystemTime() { | ||||
const auto now = std::chrono::duration_cast<T>( | const auto now = std::chrono::duration_cast<T>( | ||||
std::chrono::system_clock::now().time_since_epoch()); | std::chrono::system_clock::now().time_since_epoch()); | ||||
assert(now.count() > 0); | assert(now.count() > 0); | ||||
return now; | return now; | ||||
} | } | ||||
void SetMockTime(int64_t nMockTimeIn) { | void SetMockTime(int64_t nMockTimeIn) { | ||||
assert(nMockTimeIn >= 0); | Assert(nMockTimeIn >= 0); | ||||
nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | nMockTime.store(nMockTimeIn, std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetMockTime() { | int64_t GetMockTime() { | ||||
return nMockTime.load(std::memory_order_relaxed); | return nMockTime.load(std::memory_order_relaxed); | ||||
} | } | ||||
int64_t GetTimeMillis() { | int64_t GetTimeMillis() { | ||||
▲ Show 20 Lines • Show All 70 Lines • Show Last 20 Lines |