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 10:11:09 -04: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 00:04:01 -03: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 10:11:09 -04:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#if defined(HAVE_CONFIG_H)
|
2017-11-09 21:57:53 -03: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 10:11:09 -04:00
|
|
|
#endif
|
|
|
|
|
2018-10-22 19:51:11 -03: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 10:11:09 -04:00
|
|
|
|
2017-02-11 17:37:05 -03: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 10:11:09 -04:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2018-04-13 02:31:15 -03:00
|
|
|
#include <ctime>
|
2020-02-21 14:19:57 -03:00
|
|
|
#include <thread>
|
|
|
|
|
2018-04-13 02:31:15 -03: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 10:11:09 -04:00
|
|
|
|
2020-02-21 14:19:57 -03:00
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
|
|
|
|
2017-02-11 17:37:05 -03:00
|
|
|
static std::atomic<int64_t> nMockTime(0); //!< For unit 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 10:11:09 -04:00
|
|
|
|
|
|
|
int64_t GetTime()
|
|
|
|
{
|
2017-02-11 17:37:05 -03: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 10:11:09 -04:00
|
|
|
|
2017-08-07 01:36:37 -04:00
|
|
|
time_t now = time(nullptr);
|
2015-11-24 23:39:19 -03: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 10:11:09 -04:00
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
|
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 10:11:09 -04:00
|
|
|
void SetMockTime(int64_t nMockTimeIn)
|
|
|
|
{
|
2017-02-11 17:37:05 -03: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 10:11:09 -04:00
|
|
|
}
|
|
|
|
|
2017-05-10 16:49:00 -03:00
|
|
|
int64_t GetMockTime()
|
|
|
|
{
|
|
|
|
return nMockTime.load(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 10:11:09 -04:00
|
|
|
int64_t GetTimeMillis()
|
|
|
|
{
|
2015-11-24 23:39:19 -03:00
|
|
|
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
|
|
|
|
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds();
|
|
|
|
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 10:11:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t GetTimeMicros()
|
|
|
|
{
|
2015-11-24 23:39:19 -03:00
|
|
|
int64_t now = (boost::posix_time::microsec_clock::universal_time() -
|
|
|
|
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
|
|
|
|
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 10:11:09 -04:00
|
|
|
}
|
|
|
|
|
2017-01-19 15:01:18 -03:00
|
|
|
int64_t GetSystemTimeInSeconds()
|
|
|
|
{
|
|
|
|
return GetTimeMicros()/1000000;
|
|
|
|
}
|
|
|
|
|
2018-02-28 12:46:31 -03:00
|
|
|
std::string FormatISO8601DateTime(int64_t nTime) {
|
2018-04-13 02:31:15 -03:00
|
|
|
struct tm ts;
|
|
|
|
time_t time_val = nTime;
|
2020-03-18 19:23:25 -03:00
|
|
|
#ifdef HAVE_GMTIME_R
|
2020-02-16 08:28:37 -03:00
|
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
2020-03-18 19:23:25 -03:00
|
|
|
#else
|
|
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
2018-04-19 19:41:15 -03:00
|
|
|
#endif
|
2020-02-16 08:28:37 -03:00
|
|
|
return {};
|
|
|
|
}
|
2018-04-13 02:31:15 -03: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 12:46:31 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string FormatISO8601Date(int64_t nTime) {
|
2018-04-13 02:31:15 -03:00
|
|
|
struct tm ts;
|
|
|
|
time_t time_val = nTime;
|
2020-03-18 19:23:25 -03:00
|
|
|
#ifdef HAVE_GMTIME_R
|
2020-02-16 08:28:37 -03:00
|
|
|
if (gmtime_r(&time_val, &ts) == nullptr) {
|
2020-03-18 19:23:25 -03:00
|
|
|
#else
|
|
|
|
if (gmtime_s(&ts, &time_val) != 0) {
|
2018-04-19 19:41:15 -03:00
|
|
|
#endif
|
2020-02-16 08:28:37 -03:00
|
|
|
return {};
|
|
|
|
}
|
2018-04-13 02:31:15 -03:00
|
|
|
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
|
2018-02-28 12:46:31 -03:00
|
|
|
}
|
2019-10-26 15: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 08:28:37 -03:00
|
|
|
}
|