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
|
2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-11-17 04:04:01 +01: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.
|
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#ifndef BITCOIN_UTIL_TIME_H
|
|
|
|
#define BITCOIN_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-10-26 13:25:11 +02:00
|
|
|
#include <compat.h>
|
|
|
|
|
2022-10-15 23:56:27 +02:00
|
|
|
#include <chrono>
|
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 <stdint.h>
|
|
|
|
#include <string>
|
2022-10-15 23:56:27 +02:00
|
|
|
|
|
|
|
using namespace std::chrono_literals;
|
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
|
|
|
|
2023-07-19 19:59:54 +02:00
|
|
|
using SteadyClock = std::chrono::steady_clock;
|
2023-07-19 19:58:56 +02:00
|
|
|
using SteadySeconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::seconds>;
|
|
|
|
using SteadyMilliseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::milliseconds>;
|
|
|
|
using SteadyMicroseconds = std::chrono::time_point<std::chrono::steady_clock, std::chrono::microseconds>;
|
|
|
|
|
2021-07-13 11:31:17 +02:00
|
|
|
void UninterruptibleSleep(const std::chrono::microseconds& n);
|
|
|
|
|
2021-07-15 19:55:35 +02:00
|
|
|
/**
|
|
|
|
* Helper to count the seconds of a duration.
|
|
|
|
*
|
2022-10-30 17:42:14 +01:00
|
|
|
* All durations should be using std::chrono and calling this should generally
|
|
|
|
* be avoided in code. Though, it is still preferred to an inline t.count() to
|
|
|
|
* protect against a reliance on the exact type of t.
|
|
|
|
*
|
|
|
|
* This helper is used to convert durations before passing them over an
|
|
|
|
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
|
2021-07-15 19:55:35 +02:00
|
|
|
*/
|
2024-04-03 18:10:14 +02:00
|
|
|
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
|
|
|
|
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
|
|
|
|
constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
|
|
|
|
|
|
|
|
using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper to count the seconds in any std::chrono::duration type
|
|
|
|
*/
|
|
|
|
inline double CountSecondsDouble(SecondsDouble t) { return t.count(); }
|
2021-07-15 19:55:35 +02:00
|
|
|
|
2017-08-24 01:38:29 +02:00
|
|
|
/**
|
2019-05-29 13:39:45 +02:00
|
|
|
* DEPRECATED
|
2024-03-26 18:22:54 +01:00
|
|
|
* Use either GetTimeSeconds (not mockable) or GetTime<T> (mockable)
|
2017-08-24 01:38:29 +02: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 GetTime();
|
2019-05-29 13:39:45 +02:00
|
|
|
|
|
|
|
/** Returns the system time (not mockable) */
|
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();
|
2019-05-29 13:39:45 +02:00
|
|
|
/** Returns the system time (not mockable) */
|
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();
|
2019-05-29 13:39:45 +02:00
|
|
|
/** Returns the system time (not mockable) */
|
2024-03-26 18:22:54 +01:00
|
|
|
int64_t GetTimeSeconds(); // Like GetTime(), but not mockable
|
2019-05-29 13:39:45 +02:00
|
|
|
|
2024-01-16 21:11:49 +01:00
|
|
|
/**
|
|
|
|
* DEPRECATED
|
|
|
|
* Use SetMockTime with chrono type
|
|
|
|
*
|
|
|
|
* @param[in] nMockTimeIn Time in seconds.
|
|
|
|
*/
|
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);
|
2024-01-16 21:11:49 +01:00
|
|
|
|
|
|
|
/** For testing. Set e.g. with the setmocktime rpc, or -mocktime argument */
|
|
|
|
void SetMockTime(std::chrono::seconds mock_time_in);
|
|
|
|
|
2019-05-29 13:39:45 +02:00
|
|
|
/** For testing */
|
2024-01-16 21:11:49 +01:00
|
|
|
std::chrono::seconds GetMockTime();
|
2019-05-29 13:39:45 +02:00
|
|
|
|
|
|
|
/** Return system time (or mocked time, if set) */
|
|
|
|
template <typename T>
|
|
|
|
T GetTime();
|
2023-07-19 19:58:56 +02:00
|
|
|
/**
|
|
|
|
* Return the current time point cast to the given precicion. Only use this
|
|
|
|
* when an exact precicion is needed, otherwise use T::clock::now() directly.
|
|
|
|
*/
|
|
|
|
template <typename T>
|
|
|
|
T Now()
|
|
|
|
{
|
|
|
|
return std::chrono::time_point_cast<typename T::duration>(T::clock::now());
|
|
|
|
}
|
2019-05-29 13:39:45 +02:00
|
|
|
|
2018-03-10 14:08:15 +01:00
|
|
|
/**
|
|
|
|
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
|
|
|
|
* helper functions if possible.
|
|
|
|
*/
|
|
|
|
std::string FormatISO8601DateTime(int64_t nTime);
|
|
|
|
std::string FormatISO8601Date(int64_t nTime);
|
|
|
|
std::string FormatISO8601Time(int64_t nTime);
|
2019-10-26 20:10:44 +02:00
|
|
|
int64_t ParseISO8601DateTime(const std::string& str);
|
2018-03-10 14:08:15 +01:00
|
|
|
|
2022-10-26 13:25:11 +02:00
|
|
|
/**
|
|
|
|
* Convert milliseconds to a struct timeval for e.g. select.
|
|
|
|
*/
|
|
|
|
struct timeval MillisToTimeval(int64_t nTimeout);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert milliseconds to a struct timeval for e.g. select.
|
|
|
|
*/
|
|
|
|
struct timeval MillisToTimeval(std::chrono::milliseconds ms);
|
|
|
|
|
2021-02-17 20:23:25 +01:00
|
|
|
/** Sanity check epoch match normal Unix epoch */
|
|
|
|
bool ChronoSanityCheck();
|
|
|
|
|
2021-06-27 08:33:13 +02:00
|
|
|
#endif // BITCOIN_UTIL_TIME_H
|