Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2014-11-17 11:04:01 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
2023-12-14 18:20:48 +01:00
|
|
|
#include <util/time.h>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2022-06-28 13:27:57 +01:00
|
|
|
#include <compat/compat.h>
|
2022-06-09 16:26:55 +01:00
|
|
|
#include <tinyformat.h>
|
2021-02-01 14:57:34 +00:00
|
|
|
#include <util/check.h>
|
|
|
|
|
|
2022-06-09 16:26:55 +01:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <chrono>
|
|
|
|
|
#include <string>
|
2023-12-14 18:20:48 +01:00
|
|
|
#include <thread>
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2020-02-21 09:19:57 -08:00
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
|
|
|
|
|
2024-04-15 13:21:03 +02:00
|
|
|
static std::atomic<std::chrono::seconds> g_mock_time{}; //!< For testing
|
Split up util.cpp/h
Split up util.cpp/h into:
- string utilities (hex, base32, base64): no internal dependencies, no dependency on boost (apart from foreach)
- money utilities (parsesmoney, formatmoney)
- time utilities (gettime*, sleep, format date):
- and the rest (logging, argument parsing, config file parsing)
The latter is basically the environment and OS handling,
and is stripped of all utility functions, so we may want to
rename it to something else than util.cpp/h for clarity (Matt suggested
osinterface).
Breaks dependency of sha256.cpp on all the things pulled in by util.
2014-08-21 16:11:09 +02:00
|
|
|
|
2022-04-08 17:37:18 +02:00
|
|
|
NodeClock::time_point NodeClock::now() noexcept
|
2019-05-18 17:44:39 -04:00
|
|
|
{
|
2024-04-15 13:21:03 +02:00
|
|
|
const auto mocktime{g_mock_time.load(std::memory_order_relaxed)};
|
2022-04-09 14:41:09 +02:00
|
|
|
const auto ret{
|
2019-05-18 17:44:39 -04:00
|
|
|
mocktime.count() ?
|
|
|
|
|
mocktime :
|
2022-04-08 17:37:18 +02:00
|
|
|
std::chrono::system_clock::now().time_since_epoch()};
|
2022-04-09 14:41:09 +02:00
|
|
|
assert(ret > 0s);
|
2022-04-08 17:37:18 +02:00
|
|
|
return time_point{ret};
|
|
|
|
|
};
|
2019-05-18 17:44:39 -04:00
|
|
|
|
2024-04-15 12:57:17 +02:00
|
|
|
void SetMockTime(int64_t nMockTimeIn) { SetMockTime(std::chrono::seconds{nMockTimeIn}); }
|
2021-02-12 10:23:45 -08:00
|
|
|
void SetMockTime(std::chrono::seconds mock_time_in)
|
|
|
|
|
{
|
2024-04-15 12:57:17 +02:00
|
|
|
Assert(mock_time_in >= 0s);
|
2024-04-15 13:21:03 +02:00
|
|
|
g_mock_time.store(mock_time_in, std::memory_order_relaxed);
|
2021-02-12 10:23:45 -08:00
|
|
|
}
|
|
|
|
|
|
2021-02-10 18:30:51 -08:00
|
|
|
std::chrono::seconds GetMockTime()
|
2017-05-10 15:49:00 -04:00
|
|
|
{
|
2024-04-15 13:21:03 +02:00
|
|
|
return g_mock_time.load(std::memory_order_relaxed);
|
2017-05-10 15:49:00 -04:00
|
|
|
}
|
|
|
|
|
|
2022-04-09 14:41:09 +02:00
|
|
|
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
|
|
|
|
|
2023-12-14 18:20:48 +01:00
|
|
|
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());
|
2018-02-28 16:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-14 18:20:48 +01:00
|
|
|
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()});
|
2018-02-28 16:46:31 +01:00
|
|
|
}
|
2019-10-26 21:10:44 +03:00
|
|
|
|
2021-01-04 13:14:32 +01:00
|
|
|
struct timeval MillisToTimeval(int64_t nTimeout)
|
|
|
|
|
{
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
timeout.tv_sec = nTimeout / 1000;
|
|
|
|
|
timeout.tv_usec = (nTimeout % 1000) * 1000;
|
|
|
|
|
return timeout;
|
|
|
|
|
}
|
2020-12-23 16:40:11 +01:00
|
|
|
|
|
|
|
|
struct timeval MillisToTimeval(std::chrono::milliseconds ms)
|
|
|
|
|
{
|
|
|
|
|
return MillisToTimeval(count_milliseconds(ms));
|
|
|
|
|
}
|