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-2016 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. | ||||
#if defined(HAVE_CONFIG_H) | #if defined(HAVE_CONFIG_H) | ||||
#include <config/bitcoin-config.h> | #include <config/bitcoin-config.h> | ||||
#endif | #endif | ||||
#include <util/time.h> | #include <util/time.h> | ||||
Show All 15 Lines | if (mocktime) { | ||||
return mocktime; | return mocktime; | ||||
} | } | ||||
time_t now = time(nullptr); | time_t now = time(nullptr); | ||||
assert(now > 0); | assert(now > 0); | ||||
return now; | return now; | ||||
} | } | ||||
template <typename T> T GetTime() { | |||||
const std::chrono::seconds mocktime{ | |||||
nMockTime.load(std::memory_order_relaxed)}; | |||||
return std::chrono::duration_cast<T>( | |||||
mocktime.count() ? mocktime | |||||
: std::chrono::microseconds{GetTimeMicros()}); | |||||
} | |||||
template std::chrono::seconds GetTime(); | |||||
template std::chrono::milliseconds GetTime(); | |||||
template std::chrono::microseconds GetTime(); | |||||
void SetMockTime(int64_t nMockTimeIn) { | void SetMockTime(int64_t nMockTimeIn) { | ||||
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); | ||||
} | } | ||||
▲ Show 20 Lines • Show All 48 Lines • Show Last 20 Lines |