Skip to content

Commit 2222aec

Browse files
author
MarcoFalke
committed
util: Implement ParseISO8601DateTime based on C++20
1 parent 144f98d commit 2222aec

File tree

5 files changed

+101
-8
lines changed

5 files changed

+101
-8
lines changed

src/test/fuzz/util.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021-2022 The Bitcoin Core developers
1+
// Copyright (c) 2021-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -34,8 +34,8 @@ CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::option
3434
int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional<int64_t>& min, const std::optional<int64_t>& max) noexcept
3535
{
3636
// Avoid t=0 (1970-01-01T00:00:00Z) since SetMockTime(0) disables mocktime.
37-
static const int64_t time_min{946684801}; // 2000-01-01T00:00:01Z
38-
static const int64_t time_max{4133980799}; // 2100-12-31T23:59:59Z
37+
static const int64_t time_min{ParseISO8601DateTime("2000-01-01T00:00:01Z").value()};
38+
static const int64_t time_max{ParseISO8601DateTime("2100-12-31T23:59:59Z").value()};
3939
return fuzzed_data_provider.ConsumeIntegralInRange<int64_t>(min.value_or(time_min), max.value_or(time_max));
4040
}
4141

src/test/util_tests.cpp

+48-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2022 The Bitcoin Core developers
1+
// Copyright (c) 2011-present The Bitcoin Core developers
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -323,20 +323,65 @@ BOOST_AUTO_TEST_CASE(util_TrimString)
323323
BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), "");
324324
}
325325

326+
BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime)
327+
{
328+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1969-12-31T23:59:59Z").value(), -1);
329+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z").value(), 0);
330+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:01Z").value(), 1);
331+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z").value(), 946684801);
332+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z").value(), 1317425777);
333+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z").value(), 4133980799);
334+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("9999-12-31T23:59:59Z").value(), 253402300799);
335+
336+
// Accept edge-cases, where the time overflows. They are not produced by
337+
// FormatISO8601DateTime, so this can be changed in the future, if needed.
338+
// For now, keep compatibility with the previous implementation.
339+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:00:00Z").value(), 947041200);
340+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:99:00Z").value(), 946690740);
341+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:99Z").value(), 946684899);
342+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:99:99Z").value(), 947047239);
343+
344+
// Reject date overflows.
345+
BOOST_CHECK(!ParseISO8601DateTime("2000-99-01T00:00:00Z"));
346+
BOOST_CHECK(!ParseISO8601DateTime("2000-01-99T00:00:00Z"));
347+
348+
// Reject out-of-range years
349+
BOOST_CHECK(!ParseISO8601DateTime("32768-12-31T23:59:59Z"));
350+
BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T23:59:59Z"));
351+
BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T00:00:00Z"));
352+
BOOST_CHECK(!ParseISO8601DateTime("999-12-31T00:00:00Z"));
353+
354+
// Reject invalid format
355+
const std::string valid{"2000-01-01T00:00:01Z"};
356+
BOOST_CHECK(ParseISO8601DateTime(valid).has_value());
357+
for (auto mut{0U}; mut < valid.size(); ++mut) {
358+
std::string invalid{valid};
359+
invalid[mut] = 'a';
360+
BOOST_CHECK(!ParseISO8601DateTime(invalid));
361+
}
362+
}
363+
326364
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
327365
{
328366
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890963199), "32767-12-31T23:59:59Z");
329367
BOOST_CHECK_EQUAL(FormatISO8601DateTime(971890876800), "32767-12-31T00:00:00Z");
330-
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
368+
369+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(-1), "1969-12-31T23:59:59Z");
331370
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
371+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1), "1970-01-01T00:00:01Z");
372+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(946684801), "2000-01-01T00:00:01Z");
373+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
374+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(4133980799), "2100-12-31T23:59:59Z");
375+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(253402300799), "9999-12-31T23:59:59Z");
332376
}
333377

334378
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)
335379
{
336380
BOOST_CHECK_EQUAL(FormatISO8601Date(971890963199), "32767-12-31");
337381
BOOST_CHECK_EQUAL(FormatISO8601Date(971890876800), "32767-12-31");
338-
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
382+
339383
BOOST_CHECK_EQUAL(FormatISO8601Date(0), "1970-01-01");
384+
BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30");
340385
}
341386

342387
BOOST_AUTO_TEST_CASE(util_FormatMoney)

src/util/time.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,10 +8,13 @@
88
#include <compat/compat.h>
99
#include <tinyformat.h>
1010
#include <util/check.h>
11+
#include <util/strencodings.h>
1112

1213
#include <atomic>
1314
#include <chrono>
15+
#include <optional>
1416
#include <string>
17+
#include <string_view>
1518
#include <thread>
1619

1720
void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); }
@@ -60,6 +63,30 @@ std::string FormatISO8601Date(int64_t nTime)
6063
return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()});
6164
}
6265

66+
std::optional<int64_t> ParseISO8601DateTime(std::string_view str)
67+
{
68+
constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()};
69+
if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') {
70+
return {};
71+
}
72+
const auto year{ToIntegral<uint16_t>(str.substr(0, 4))};
73+
const auto month{ToIntegral<uint8_t>(str.substr(5, 2))};
74+
const auto day{ToIntegral<uint8_t>(str.substr(8, 2))};
75+
const auto hour{ToIntegral<uint8_t>(str.substr(11, 2))};
76+
const auto min{ToIntegral<uint8_t>(str.substr(14, 2))};
77+
const auto sec{ToIntegral<uint8_t>(str.substr(17, 2))};
78+
if (!year || !month || !day || !hour || !min || !sec) {
79+
return {};
80+
}
81+
const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}};
82+
if (!ymd.ok()) {
83+
return {};
84+
}
85+
const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}};
86+
const auto tp{std::chrono::sys_days{ymd} + time};
87+
return int64_t{TicksSinceEpoch<std::chrono::seconds>(tp)};
88+
}
89+
6390
struct timeval MillisToTimeval(int64_t nTimeout)
6491
{
6592
struct timeval timeout;

src/util/time.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) 2009-2010 Satoshi Nakamoto
2-
// Copyright (c) 2009-2022 The Bitcoin Core developers
2+
// Copyright (c) 2009-present The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

@@ -8,7 +8,9 @@
88

99
#include <chrono> // IWYU pragma: export
1010
#include <cstdint>
11+
#include <optional>
1112
#include <string>
13+
#include <string_view>
1214

1315
using namespace std::chrono_literals;
1416

@@ -105,6 +107,7 @@ T GetTime()
105107
*/
106108
std::string FormatISO8601DateTime(int64_t nTime);
107109
std::string FormatISO8601Date(int64_t nTime);
110+
std::optional<int64_t> ParseISO8601DateTime(std::string_view str);
108111

109112
/**
110113
* Convert milliseconds to a struct timeval for e.g. select.

src/wallet/test/rpc_util_tests.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,27 @@ BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime)
1414
{
1515
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
1616
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
17+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:01Z"), 1);
1718
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z"), 946684801);
1819
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
1920
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z"), 4133980799);
21+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("9999-12-31T23:59:59Z"), 253402300799);
22+
23+
// Accept edge-cases, where the time overflows.
24+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:00:00Z"), 947041200);
25+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:99:00Z"), 946690740);
26+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:99Z"), 946684899);
27+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:99:99Z"), 947047239);
28+
29+
// Reject date overflows.
30+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-99-01T00:00:00Z"), 0);
31+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-99T00:00:00Z"), 0);
32+
33+
// Reject out-of-range years
34+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("32768-12-31T23:59:59Z"), 0);
35+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("32767-12-31T23:59:59Z"), 0);
36+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("32767-12-31T00:00:00Z"), 0);
37+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("999-12-31T00:00:00Z"), 0);
2038
}
2139

2240
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)