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
|
2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2009-2020 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.
|
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <config/bitcoin-config.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
|
|
|
#endif
|
|
|
|
|
2021-01-04 13:14:32 +01:00
|
|
|
#include <compat.h>
|
2018-10-22 15:51:11 -07: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
|
|
|
|
2021-02-01 14:57:34 +00:00
|
|
|
#include <util/check.h>
|
|
|
|
|
2017-02-11 12:37:05 -08:00
|
|
|
#include <atomic>
|
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
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2018-04-12 22:31:15 -07:00
|
|
|
#include <ctime>
|
2020-02-21 09:19:57 -08:00
|
|
|
#include <thread>
|
|
|
|
|
2018-04-12 22:31:15 -07:00
|
|
|
#include <tinyformat.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
|
|
|
|
2020-02-21 09:19:57 -08:00
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
|
|
|
|
2021-02-01 14:57:34 +00:00
|
|
|
static std::atomic<int64_t> nMockTime(0); //!< 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
|
|
|
|
|
|
|
int64_t GetTime()
|
|
|
|
{
|
2017-02-11 12:37:05 -08:00
|
|
|
int64_t mocktime = nMockTime.load(std::memory_order_relaxed);
|
|
|
|
if (mocktime) return mocktime;
|
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
|
|
|
|
2017-08-07 07:36:37 +02:00
|
|
|
time_t now = time(nullptr);
|
2015-11-24 18:39:19 -08:00
|
|
|
assert(now > 0);
|
|
|
|
return now;
|
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
|
|
|
}
|
|
|
|
|
2017-01-16 15:01:37 -05:00
|
|
|
bool ChronoSanityCheck()
|
|
|
|
{
|
|
|
|
// std::chrono::system_clock.time_since_epoch and time_t(0) are not guaranteed
|
|
|
|
// to use the Unix epoch timestamp, prior to C++20, but in practice they almost
|
|
|
|
// certainly will. Any differing behavior will be assumed to be an error, unless
|
|
|
|
// certain platforms prove to consistently deviate, at which point we'll cope
|
|
|
|
// with it by adding offsets.
|
|
|
|
|
|
|
|
// Create a new clock from time_t(0) and make sure that it represents 0
|
|
|
|
// seconds from the system_clock's time_since_epoch. Then convert that back
|
|
|
|
// to a time_t and verify that it's the same as before.
|
|
|
|
const time_t time_t_epoch{};
|
|
|
|
auto clock = std::chrono::system_clock::from_time_t(time_t_epoch);
|
|
|
|
if (std::chrono::duration_cast<std::chrono::seconds>(clock.time_since_epoch()).count() != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t time_val = std::chrono::system_clock::to_time_t(clock);
|
|
|
|
if (time_val != time_t_epoch) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the above zero time is actually equal to the known unix timestamp.
|
|
|
|
struct tm epoch;
|
|
|
|
#ifdef HAVE_GMTIME_R
|
|
|
|
if (gmtime_r(&time_val, &epoch) == nullptr) {
|
|
|
|
#else
|
|
|
|
if (gmtime_s(&epoch, &time_val) != 0) {
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((epoch.tm_sec != 0) ||
|
|
|
|
(epoch.tm_min != 0) ||
|
|
|
|
(epoch.tm_hour != 0) ||
|
|
|
|
(epoch.tm_mday != 1) ||
|
|
|
|
(epoch.tm_mon != 0) ||
|
|
|
|
(epoch.tm_year != 70)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-05-18 17:44:39 -04:00
|
|
|
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();
|
|
|
|
|
2020-02-21 13:12:40 -05:00
|
|
|
template <typename T>
|
|
|
|
static T GetSystemTime()
|
|
|
|
{
|
|
|
|
const auto now = std::chrono::duration_cast<T>(std::chrono::system_clock::now().time_since_epoch());
|
|
|
|
assert(now.count() > 0);
|
|
|
|
return now;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void SetMockTime(int64_t nMockTimeIn)
|
|
|
|
{
|
2021-02-01 14:57:34 +00:00
|
|
|
Assert(nMockTimeIn >= 0);
|
2017-02-11 12:37:05 -08:00
|
|
|
nMockTime.store(nMockTimeIn, std::memory_order_relaxed);
|
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
|
|
|
}
|
|
|
|
|
2021-02-12 10:23:45 -08:00
|
|
|
void SetMockTime(std::chrono::seconds mock_time_in)
|
|
|
|
{
|
|
|
|
nMockTime.store(mock_time_in.count(), std::memory_order_relaxed);
|
|
|
|
}
|
|
|
|
|
2021-02-10 18:30:51 -08:00
|
|
|
std::chrono::seconds GetMockTime()
|
2017-05-10 15:49:00 -04:00
|
|
|
{
|
2021-02-10 18:30:51 -08:00
|
|
|
return std::chrono::seconds(nMockTime.load(std::memory_order_relaxed));
|
2017-05-10 15:49:00 -04:00
|
|
|
}
|
|
|
|
|
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
|
|
|
int64_t GetTimeMillis()
|
|
|
|
{
|
2020-02-21 13:12:40 -05:00
|
|
|
return int64_t{GetSystemTime<std::chrono::milliseconds>().count()};
|
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
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetTimeMicros()
|
|
|
|
{
|
2020-02-21 13:12:40 -05:00
|
|
|
return int64_t{GetSystemTime<std::chrono::microseconds>().count()};
|
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
|
|
|
}
|
|
|
|
|
2021-03-30 15:19:01 +08:00
|
|
|
int64_t GetTimeSeconds()
|
2017-01-19 13:01:18 -05:00
|
|
|
{
|
2020-02-21 13:12:40 -05:00
|
|
|
return int64_t{GetSystemTime<std::chrono::seconds>().count()};
|
2017-01-19 13:01:18 -05:00
|
|
|
}
|
|
|
|
|
2018-02-28 16:46:31 +01:00
|
|
|
std::string FormatISO8601DateTime(int64_t nTime) {
|
2018-04-12 22:31:15 -07:00
|
|
|
struct tm ts;
|
|
|
|
time_t time_val = nTime;
|
2020-03-18 15:23:25 -07:00
|
|
|
#ifdef HAVE_GMTIME_R
|
2020-02-16 11:28:37 +00:00
|
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
2020-03-18 15:23:25 -07:00
|
|
|
#else
|
|
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
2018-04-20 08:41:15 +10:00
|
|
|
#endif
|
2020-02-16 11:28:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
2018-04-12 22:31:15 -07:00
|
|
|
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);
|
2018-02-28 16:46:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FormatISO8601Date(int64_t nTime) {
|
2018-04-12 22:31:15 -07:00
|
|
|
struct tm ts;
|
|
|
|
time_t time_val = nTime;
|
2020-03-18 15:23:25 -07:00
|
|
|
#ifdef HAVE_GMTIME_R
|
2020-02-16 11:28:37 +00:00
|
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
2020-03-18 15:23:25 -07:00
|
|
|
#else
|
|
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
2018-04-20 08:41:15 +10:00
|
|
|
#endif
|
2020-02-16 11:28:37 +00:00
|
|
|
return {};
|
|
|
|
}
|
2018-04-12 22:31:15 -07:00
|
|
|
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
|
2018-02-28 16:46:31 +01:00
|
|
|
}
|
2019-10-26 21:10:44 +03:00
|
|
|
|
|
|
|
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();
|
2020-02-16 11:28:37 +00: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));
|
|
|
|
}
|