Skip to content

Commit fa72dcb

Browse files
author
MarcoFalke
committed
refactor: FormatISO8601* without gmtime*
1 parent fa2c486 commit fa72dcb

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

src/test/util_tests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,18 @@ BOOST_AUTO_TEST_CASE(util_TrimString)
290290

291291
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
292292
{
293+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890963199), "32767-12-31T23:59:59Z");
294+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890876800), "32767-12-31T00:00:00Z");
293295
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
294296
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
295297
}
296298

297299
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
298300
{
301+
BOOST_CHECK_EQUAL(FormatISO8601Date(971890963199), "32767-12-31");
302+
BOOST_CHECK_EQUAL(FormatISO8601Date(971890876800), "32767-12-31");
299303
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
304+
BOOST_CHECK_EQUAL(FormatISO8601Date(0), "1970-01-01");
300305
}
301306

302307
BOOST_AUTO_TEST_CASE(util_FormatMoney)

src/util/time.cpp

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

6-
#if defined(HAVE_CONFIG_H)
7-
#include <config/bitcoin-config.h>
8-
#endif
6+
#include <util/time.h>
97

108
#include <compat/compat.h>
119
#include <tinyformat.h>
12-
#include <util/time.h>
1310
#include <util/check.h>
1411

1512
#include <atomic>
1613
#include <chrono>
17-
#include <ctime>
18-
#include <locale>
19-
#include <thread>
20-
#include <sstream>
2114
#include <string>
15+
#include <thread>
2216

2317
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
2418

@@ -53,30 +47,21 @@ std::chrono::seconds GetMockTime()
5347

5448
int64_t GetTime() { return GetTime<std::chrono::seconds>().count(); }
5549

56-
std::string FormatISO8601DateTime(int64_t nTime) {
57-
struct tm ts;
58-
time_t time_val = nTime;
59-
#ifdef HAVE_GMTIME_R
60-
if (gmtime_r(&time_val, &ts) == nullptr) {
61-
#else
62-
if (gmtime_s(&ts, &time_val) != 0) {
63-
#endif
64-
return {};
65-
}
66-
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);
50+
std::string FormatISO8601DateTime(int64_t nTime)
51+
{
52+
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
53+
const auto days{std::chrono::floor<std::chrono::days>(secs)};
54+
const std::chrono::year_month_day ymd{days};
55+
const std::chrono::hh_mm_ss hms{secs - days};
56+
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());
6757
}
6858

69-
std::string FormatISO8601Date(int64_t nTime) {
70-
struct tm ts;
71-
time_t time_val = nTime;
72-
#ifdef HAVE_GMTIME_R
73-
if (gmtime_r(&time_val, &ts) == nullptr) {
74-
#else
75-
if (gmtime_s(&ts, &time_val) != 0) {
76-
#endif
77-
return {};
78-
}
79-
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
59+
std::string FormatISO8601Date(int64_t nTime)
60+
{
61+
const std::chrono::sys_seconds secs{std::chrono::seconds{nTime}};
62+
const auto days{std::chrono::floor<std::chrono::days>(secs)};
63+
const std::chrono::year_month_day ymd{days};
64+
return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
8065
}
8166

8267
struct timeval MillisToTimeval(int64_t nTimeout)

0 commit comments

Comments
 (0)