mirror of
https://github.com/dashpay/dash.git
synced 2024-12-28 13:32:47 +01:00
4aa197dbdb
fa4632c41714dfaa699bacc6a947d72668a4deef test: Move boost/stdlib includes last (MarcoFalke) fa488f131fd4f5bab0d01376c5a5013306f1abcd scripted-diff: Bump copyright headers (MarcoFalke) fac5c373006a9e4bcbb56843bb85f1aca4d87599 scripted-diff: Sort test includes (MarcoFalke) Pull request description: When writing tests, often includes need to be added or removed. Currently the list of includes is not sorted, so developers that write tests and have `clang-format` installed will either have an unrelated change (sorting) included in their commit or they will have to manually undo the sort. This pull preempts both issues by just sorting all includes in one commit. Please be aware that this is **NOT** a change to policy to enforce clang-format or any other developer guideline or process. Developers are free to use whatever tool they want, see also #18651. Edit: Also includes a commit to bump the copyright headers, so that the touched files don't need to be touched again for that. ACKs for top commit: practicalswift: ACK fa4632c41714dfaa699bacc6a947d72668a4deef jonatack: ACK fa4632c41714dfaa, light review and sanity checks with gcc build and clang fuzz build Tree-SHA512: 130a8d073a379ba556b1e64104d37c46b671425c0aef0ed725fd60156a95e8dc83fb6f0b5330b2f8152cf5daaf3983b4aca5e75812598f2626c39fd12b88b180
140 lines
3.6 KiB
C++
140 lines
3.6 KiB
C++
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
|
// Distributed under the MIT software license, see the accompanying
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
#include <config/bitcoin-config.h>
|
|
#endif
|
|
|
|
#include <compat.h>
|
|
#include <util/time.h>
|
|
|
|
#include <atomic>
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
|
#include <ctime>
|
|
#include <thread>
|
|
|
|
#include <tinyformat.h>
|
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
|
|
|
static std::atomic<int64_t> nMockTime(0); //!< For unit 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;
|
|
}
|
|
|
|
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)
|
|
{
|
|
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
|
|
}
|
|
|
|
int64_t GetMockTime()
|
|
{
|
|
return nMockTime.load(std::memory_order_relaxed);
|
|
}
|
|
|
|
int64_t GetTimeMillis()
|
|
{
|
|
int64_t now = std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
|
|
assert(now > 0);
|
|
return now;
|
|
}
|
|
|
|
int64_t GetTimeMicros()
|
|
{
|
|
int64_t now = std::chrono::time_point_cast<std::chrono::microseconds>(std::chrono::system_clock::now()).time_since_epoch().count();
|
|
assert(now > 0);
|
|
return now;
|
|
}
|
|
|
|
int64_t GetSystemTimeInSeconds()
|
|
{
|
|
return GetTimeMicros()/1000000;
|
|
}
|
|
|
|
std::string FormatISO8601DateTime(int64_t nTime) {
|
|
struct tm ts;
|
|
time_t time_val = nTime;
|
|
#ifdef HAVE_GMTIME_R
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
|
#else
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
|
#endif
|
|
return {};
|
|
}
|
|
return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
|
|
}
|
|
|
|
std::string FormatISO8601Date(int64_t nTime) {
|
|
struct tm ts;
|
|
time_t time_val = nTime;
|
|
#ifdef HAVE_GMTIME_R
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
|
#else
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
|
#endif
|
|
return {};
|
|
}
|
|
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
|
|
}
|
|
|
|
std::string FormatISO8601Time(int64_t nTime) {
|
|
struct tm ts;
|
|
time_t time_val = nTime;
|
|
#ifdef HAVE_GMTIME_R
|
|
gmtime_r(&time_val, &ts);
|
|
#else
|
|
gmtime_s(&ts, &time_val);
|
|
#endif
|
|
return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
|
|
}
|
|
|
|
int64_t ParseISO8601DateTime(const std::string& str)
|
|
{
|
|
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
|
|
static const std::locale loc(std::locale::classic(),
|
|
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
|
|
std::istringstream iss(str);
|
|
iss.imbue(loc);
|
|
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
|
|
iss >> ptime;
|
|
if (ptime.is_not_a_date_time() || epoch > ptime)
|
|
return 0;
|
|
return (ptime - epoch).total_seconds();
|
|
}
|
|
|
|
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));
|
|
}
|