mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-05-07 02:11:06 +02:00
79 lines
2.4 KiB
C++
79 lines
2.4 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#include <util/time.h>
|
|
|
|
#include <compat/compat.h>
|
|
#include <tinyformat.h>
|
|
#include <util/check.h>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <string>
|
|
#include <thread>
|
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
|
|
|
static std::atomic<int64_t> nMockTime(0); //!< For testing
|
|
|
|
NodeClock::time_point NodeClock::now() noexcept
|
|
{
|
|
const std::chrono::seconds mocktime{nMockTime.load(std::memory_order_relaxed)};
|
|
const auto ret{
|
|
mocktime.count() ?
|
|
mocktime :
|
|
std::chrono::system_clock::now().time_since_epoch()};
|
|
assert(ret > 0s);
|
|
return time_point{ret};
|
|
};
|
|
|
|
void SetMockTime(int64_t nMockTimeIn)
|
|
{
|
|
Assert(nMockTimeIn >= 0);
|
|
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
|
|
}
|
|
|
|
void SetMockTime(std::chrono::seconds mock_time_in)
|
|
{
|
|
nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
|
|
}
|
|
|
|
std::chrono::seconds GetMockTime()
|
|
{
|
|
return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
|
|
}
|
|
|
|
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
|
|
|
std::string FormatISO8601DateTime(int64_t nTime)
|
|
{
|
|
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
|
|
const auto days{std::chrono::floor<std::chrono::days>(secs)};
|
|
const std::chrono::year_month_day ymd{days};
|
|
const std::chrono::hh_mm_ss hms{secs - days};
|
|
return strprintf("%04i-%02u-%02uT%02i:%02i:%02iZ", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}, hms.hours().count(), hms.minutes().count(), hms.seconds().count());
|
|
}
|
|
|
|
std::string FormatISO8601Date(int64_t nTime)
|
|
{
|
|
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
|
|
const auto days{std::chrono::floor<std::chrono::days>(secs)};
|
|
const std::chrono::year_month_day ymd{days};
|
|
return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
|
|
}
|
|
|
|
struct timeval MillisToTimeval(int64_t nTimeout)
|
|
{
|
|
struct timeval timeout;
|
|
timeout.tv_sec = nTimeout / 1000;
|
|
timeout.tv_usec = (nTimeout % 1000) * 1000;
|
|
return timeout;
|
|
}
|
|
|
|
struct timeval MillisToTimeval(std::chrono::milliseconds ms)
|
|
{
|
|
return MillisToTimeval(count_milliseconds(ms));
|
|
}
|