mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-15 04:11:02 +01:00
refactor: FormatISO8601* without gmtime*
This commit is contained in:
parent
fa2c486afc
commit
fa72dcbfa5
2 changed files with 20 additions and 30 deletions
|
@ -290,13 +290,18 @@ BOOST_AUTO_TEST_CASE(util_TrimString)
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
|
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
|
||||||
{
|
{
|
||||||
|
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890963199), "32767-12-31T23:59:59Z");
|
||||||
|
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890876800), "32767-12-31T00:00:00Z");
|
||||||
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
|
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
|
||||||
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
|
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
|
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
|
||||||
{
|
{
|
||||||
|
BOOST_CHECK_EQUAL(FormatISO8601Date(971890963199), "32767-12-31");
|
||||||
|
BOOST_CHECK_EQUAL(FormatISO8601Date(971890876800), "32767-12-31");
|
||||||
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
|
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
|
||||||
|
BOOST_CHECK_EQUAL(FormatISO8601Date(0), "1970-01-01");
|
||||||
}
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(util_FormatMoney)
|
BOOST_AUTO_TEST_CASE(util_FormatMoney)
|
||||||
|
|
|
@ -3,22 +3,16 @@
|
||||||
// Distributed under the MIT software license, see the accompanying
|
// Distributed under the MIT software license, see the accompanying
|
||||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||||
|
|
||||||
#if defined(HAVE_CONFIG_H)
|
#include <util/time.h>
|
||||||
#include <config/bitcoin-config.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <compat/compat.h>
|
#include <compat/compat.h>
|
||||||
#include <tinyformat.h>
|
#include <tinyformat.h>
|
||||||
#include <util/time.h>
|
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
|
||||||
#include <locale>
|
|
||||||
#include <thread>
|
|
||||||
#include <sstream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
|
||||||
|
|
||||||
|
@ -53,30 +47,21 @@ std::chrono::seconds GetMockTime()
|
||||||
|
|
||||||
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
|
||||||
|
|
||||||
std::string FormatISO8601DateTime(int64_t nTime) {
|
std::string FormatISO8601DateTime(int64_t nTime)
|
||||||
struct tm ts;
|
{
|
||||||
time_t time_val = nTime;
|
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
|
||||||
#ifdef HAVE_GMTIME_R
|
const auto days{std::chrono::floor<std::chrono::days>(secs)};
|
||||||
if (gmtime_r(&time_val, &ts) == nullptr) {
|
const std::chrono::year_month_day ymd{days};
|
||||||
#else
|
const std::chrono::hh_mm_ss hms{secs - days};
|
||||||
if (gmtime_s(&ts, &time_val) != 0) {
|
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());
|
||||||
#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) {
|
std::string FormatISO8601Date(int64_t nTime)
|
||||||
struct tm ts;
|
{
|
||||||
time_t time_val = nTime;
|
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
|
||||||
#ifdef HAVE_GMTIME_R
|
const auto days{std::chrono::floor<std::chrono::days>(secs)};
|
||||||
if (gmtime_r(&time_val, &ts) == nullptr) {
|
const std::chrono::year_month_day ymd{days};
|
||||||
#else
|
return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct timeval MillisToTimeval(int64_t nTimeout)
|
struct timeval MillisToTimeval(int64_t nTimeout)
|
||||||
|
|
Loading…
Add table
Reference in a new issue