From 2222aecd5f8059785e655da7b7e3fcc59204245c Mon Sep 17 00:00:00 2001 From: MarcoFalke <*~=`'#}+{/-|&$^_@721217.xyz> Date: Thu, 28 Nov 2024 13:08:10 +0100 Subject: [PATCH] util: Implement ParseISO8601DateTime based on C++20 --- src/test/fuzz/util.cpp | 6 ++-- src/test/util_tests.cpp | 51 ++++++++++++++++++++++++++++-- src/util/time.cpp | 29 ++++++++++++++++- src/util/time.h | 5 ++- src/wallet/test/rpc_util_tests.cpp | 18 +++++++++++ 5 files changed, 101 insertions(+), 8 deletions(-) diff --git a/src/test/fuzz/util.cpp b/src/test/fuzz/util.cpp index 425b9559a77..d700e5ac975 100644 --- a/src/test/fuzz/util.cpp +++ b/src/test/fuzz/util.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2021-2022 The Bitcoin Core developers +// Copyright (c) 2021-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -34,8 +34,8 @@ CAmount ConsumeMoney(FuzzedDataProvider& fuzzed_data_provider, const std::option int64_t ConsumeTime(FuzzedDataProvider& fuzzed_data_provider, const std::optional& min, const std::optional& max) noexcept { // Avoid t=0 (1970-01-01T00:00:00Z) since SetMockTime(0) disables mocktime. - static const int64_t time_min{946684801}; // 2000-01-01T00:00:01Z - static const int64_t time_max{4133980799}; // 2100-12-31T23:59:59Z + static const int64_t time_min{ParseISO8601DateTime("2000-01-01T00:00:01Z").value()}; + static const int64_t time_max{ParseISO8601DateTime("2100-12-31T23:59:59Z").value()}; return fuzzed_data_provider.ConsumeIntegralInRange(min.value_or(time_min), max.value_or(time_max)); } diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 1624fb8b5b6..3f6e5b1b661 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1,4 +1,4 @@ -// Copyright (c) 2011-2022 The Bitcoin Core developers +// Copyright (c) 2011-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -323,20 +323,65 @@ BOOST_AUTO_TEST_CASE(util_TrimString) BOOST_CHECK_EQUAL(TrimStringView(std::string("\x05\x04\x03\x02\x01\x00", 6), std::string("\x05\x04\x03\x02\x01\x00", 6)), ""); } +BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime) +{ + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1969-12-31T23:59:59Z").value(), -1); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z").value(), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:01Z").value(), 1); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z").value(), 946684801); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z").value(), 1317425777); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z").value(), 4133980799); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("9999-12-31T23:59:59Z").value(), 253402300799); + + // Accept edge-cases, where the time overflows. They are not produced by + // FormatISO8601DateTime, so this can be changed in the future, if needed. + // For now, keep compatibility with the previous implementation. + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:00:00Z").value(), 947041200); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:99:00Z").value(), 946690740); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:99Z").value(), 946684899); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:99:99Z").value(), 947047239); + + // Reject date overflows. + BOOST_CHECK(!ParseISO8601DateTime("2000-99-01T00:00:00Z")); + BOOST_CHECK(!ParseISO8601DateTime("2000-01-99T00:00:00Z")); + + // Reject out-of-range years + BOOST_CHECK(!ParseISO8601DateTime("32768-12-31T23:59:59Z")); + BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T23:59:59Z")); + BOOST_CHECK(!ParseISO8601DateTime("32767-12-31T00:00:00Z")); + BOOST_CHECK(!ParseISO8601DateTime("999-12-31T00:00:00Z")); + + // Reject invalid format + const std::string valid{"2000-01-01T00:00:01Z"}; + BOOST_CHECK(ParseISO8601DateTime(valid).has_value()); + for (auto mut{0U}; mut < valid.size(); ++mut) { + std::string invalid{valid}; + invalid[mut] = 'a'; + BOOST_CHECK(!ParseISO8601DateTime(invalid)); + } +} + 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(-1), "1969-12-31T23:59:59Z"); BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z"); + BOOST_CHECK_EQUAL(FormatISO8601DateTime(1), "1970-01-01T00:00:01Z"); + BOOST_CHECK_EQUAL(FormatISO8601DateTime(946684801), "2000-01-01T00:00:01Z"); + BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z"); + BOOST_CHECK_EQUAL(FormatISO8601DateTime(4133980799), "2100-12-31T23:59:59Z"); + BOOST_CHECK_EQUAL(FormatISO8601DateTime(253402300799), "9999-12-31T23:59:59Z"); } 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(0), "1970-01-01"); + BOOST_CHECK_EQUAL(FormatISO8601Date(1317425777), "2011-09-30"); } BOOST_AUTO_TEST_CASE(util_FormatMoney) diff --git a/src/util/time.cpp b/src/util/time.cpp index f08eb5300a2..e20f30a4745 100644 --- a/src/util/time.cpp +++ b/src/util/time.cpp @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -8,10 +8,13 @@ #include #include #include +#include #include #include +#include #include +#include #include void UninterruptibleSleep(const std::chrono::microseconds& n) { std::this_thread::sleep_for(n); } @@ -60,6 +63,30 @@ std::string FormatISO8601Date(int64_t nTime) return strprintf("%04i-%02u-%02u", signed{ymd.year()}, unsigned{ymd.month()}, unsigned{ymd.day()}); } +std::optional ParseISO8601DateTime(std::string_view str) +{ + constexpr auto FMT_SIZE{std::string_view{"2000-01-01T01:01:01Z"}.size()}; + if (str.size() != FMT_SIZE || str[4] != '-' || str[7] != '-' || str[10] != 'T' || str[13] != ':' || str[16] != ':' || str[19] != 'Z') { + return {}; + } + const auto year{ToIntegral(str.substr(0, 4))}; + const auto month{ToIntegral(str.substr(5, 2))}; + const auto day{ToIntegral(str.substr(8, 2))}; + const auto hour{ToIntegral(str.substr(11, 2))}; + const auto min{ToIntegral(str.substr(14, 2))}; + const auto sec{ToIntegral(str.substr(17, 2))}; + if (!year || !month || !day || !hour || !min || !sec) { + return {}; + } + const std::chrono::year_month_day ymd{std::chrono::year{*year}, std::chrono::month{*month}, std::chrono::day{*day}}; + if (!ymd.ok()) { + return {}; + } + const auto time{std::chrono::hours{*hour} + std::chrono::minutes{*min} + std::chrono::seconds{*sec}}; + const auto tp{std::chrono::sys_days{ymd} + time}; + return int64_t{TicksSinceEpoch(tp)}; +} + struct timeval MillisToTimeval(int64_t nTimeout) { struct timeval timeout; diff --git a/src/util/time.h b/src/util/time.h index 108560e0e0c..27cbe50581f 100644 --- a/src/util/time.h +++ b/src/util/time.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2022 The Bitcoin Core developers +// Copyright (c) 2009-present The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -8,7 +8,9 @@ #include // IWYU pragma: export #include +#include #include +#include using namespace std::chrono_literals; @@ -105,6 +107,7 @@ T GetTime() */ std::string FormatISO8601DateTime(int64_t nTime); std::string FormatISO8601Date(int64_t nTime); +std::optional ParseISO8601DateTime(std::string_view str); /** * Convert milliseconds to a struct timeval for e.g. select. diff --git a/src/wallet/test/rpc_util_tests.cpp b/src/wallet/test/rpc_util_tests.cpp index 32f6f5ab465..6541323b4b5 100644 --- a/src/wallet/test/rpc_util_tests.cpp +++ b/src/wallet/test/rpc_util_tests.cpp @@ -14,9 +14,27 @@ BOOST_AUTO_TEST_CASE(util_ParseISO8601DateTime) { BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0); BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:01Z"), 1); BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:01Z"), 946684801); BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777); BOOST_CHECK_EQUAL(ParseISO8601DateTime("2100-12-31T23:59:59Z"), 4133980799); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("9999-12-31T23:59:59Z"), 253402300799); + + // Accept edge-cases, where the time overflows. + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:00:00Z"), 947041200); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:99:00Z"), 946690740); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T00:00:99Z"), 946684899); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-01T99:99:99Z"), 947047239); + + // Reject date overflows. + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-99-01T00:00:00Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2000-01-99T00:00:00Z"), 0); + + // Reject out-of-range years + BOOST_CHECK_EQUAL(ParseISO8601DateTime("32768-12-31T23:59:59Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("32767-12-31T23:59:59Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("32767-12-31T00:00:00Z"), 0); + BOOST_CHECK_EQUAL(ParseISO8601DateTime("999-12-31T00:00:00Z"), 0); } BOOST_AUTO_TEST_SUITE_END()