mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-06 14:19:59 -05:00
Simplify GetTime
This commit is contained in:
parent
6be319beb8
commit
0000a63689
1 changed files with 6 additions and 13 deletions
|
@ -23,16 +23,6 @@ void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread
|
||||||
|
|
||||||
static std::atomic<int64_t> nMockTime(0); //!< For testing
|
static std::atomic<int64_t> nMockTime(0); //!< For testing
|
||||||
|
|
||||||
int64_t GetTime()
|
|
||||||
{
|
|
||||||
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
|
|
||||||
if (mocktime) return mocktime;
|
|
||||||
|
|
||||||
time_t now = time(nullptr);
|
|
||||||
assert(now > 0);
|
|
||||||
return now;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ChronoSanityCheck()
|
bool ChronoSanityCheck()
|
||||||
{
|
{
|
||||||
// std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
|
// std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
|
||||||
|
@ -80,11 +70,12 @@ template <typename T>
|
||||||
T GetTime()
|
T GetTime()
|
||||||
{
|
{
|
||||||
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
|
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
|
||||||
|
const auto ret{
|
||||||
return std::chrono::duration_cast<T>(
|
|
||||||
mocktime.count() ?
|
mocktime.count() ?
|
||||||
mocktime :
|
mocktime :
|
||||||
std::chrono::microseconds{GetTimeMicros()});
|
std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch())};
|
||||||
|
assert(ret > 0s);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
template std::chrono::seconds GetTime();
|
template std::chrono::seconds GetTime();
|
||||||
template std::chrono::milliseconds GetTime();
|
template std::chrono::milliseconds GetTime();
|
||||||
|
@ -129,6 +120,8 @@ int64_t GetTimeSeconds()
|
||||||
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
|
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
||||||
|
|
||||||
std::string FormatISO8601DateTime(int64_t nTime) {
|
std::string FormatISO8601DateTime(int64_t nTime) {
|
||||||
struct tm ts;
|
struct tm ts;
|
||||||
time_t time_val = nTime;
|
time_t time_val = nTime;
|
||||||
|
|
Loading…
Add table
Reference in a new issue