As discussed in #17245.
- Renamed the function.
- Moved it from
rpcdump.cpptotime.cpp. - Added a check if the time is less then epoch return 0 to prevent an overflow.
- Added more edge cases tests and a roundtrip test.
(Fixed the whitespaces already. not sure what's up with travis)
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
47 | @@ -48,5 +48,6 @@ T GetTime(); 48 | */ 49 | std::string FormatISO8601DateTime(int64_t nTime); 50 | std::string FormatISO8601Date(int64_t nTime); 51 | +int64_t ParseISO8601DateTime(const std::string &str);
int64_t ParseISO8601DateTime(const std::string& str);
You may remove trailing whitespace with clang-format by running the https://github.com/bitcoin/bitcoin/tree/master/contrib/devtools#clang-format-diffpy script
Thanks :)
ACK e7b02b54ccfb6b2e119a67799220f8d8d8b5cccd
122 | + boost::posix_time::ptime ptime(boost::date_time::not_a_date_time); 123 | + iss >> ptime; 124 | + if (ptime.is_not_a_date_time() || epoch > ptime) 125 | + return 0; 126 | + return (ptime - epoch).total_seconds(); 127 | +}
Nit: Missing newline at end of file :)
ACK modulo nit
144 | @@ -145,9 +145,17 @@ BOOST_AUTO_TEST_CASE(util_Join) 145 | BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR"); 146 | } 147 | 148 | -BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime) 149 | +BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
What is FormatParseISO8601DateTime? I suggest add a separate test case for ParseISO8601DateTime.
Well, if I'll do so at which one of them should I add the round trip test? :)
To the new case IMO.
I think testing both in one test case is fine in this case. It's not like it's a huge function that needs to be broken up.
Yeah it's fine, just that we name cases like "util_{function}".
Code review ACK e7b02b54ccfb6b2e119a67799220f8d8d8b5cccd. Moved code is correct, left a comment regarding the test change.
151 | BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z"); 152 | + BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z"); 153 | + 154 | + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0); 155 | + BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0); 156 | + BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
Consider adding some malformed inputs too to make sure we're not only testing the happy paths :)
Well apparently boost overflows the time (i.e. 70 minutes). so the malformed ones are a problem.
I might try to fix that in a future PR together with more test cases
I think this is good to go. Let's not make a PHD thesis out of a refactoring pull.
ACK e7b02b54ccfb6b2e119a67799220f8d8d8b5cccd
<details><summary>Show signature and timestamp</summary>
Signature:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
ACK e7b02b54ccfb6b2e119a67799220f8d8d8b5cccd
-----BEGIN PGP SIGNATURE-----
iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
pUjLfgv+N9UU0SzxAKf0FRYvyT/bhaXaiZZ6WgODU8MZffV9imON/+O47MsTOF12
If2MPrpgSn9KUlqqnfRrYEPkrvC6GG//w63vmZ6JpDgwIQptgZPvddxJF3FPOvay
6sptCfj/ntE+EYnb2Jb3Zw+1FOuSt5le36KbcNlEzSC3eDK71THEZ5/KqwvmCLFD
CSdVzTCfh3wq+FZdChQUnpzb5E8BKTFjw3oKp9Sph9GJ6DCBZlMEb95LjJnZEx5i
u3BDMq8TtC2xNDDwm6WllsFhMqWufXwhwUEy+ARK08xd9/HpovrKHax27aSEf/qc
UG9bf4ITedUh6zKgF9W4C15gnWa9QL/gtyq+bMIC4Rfzl8ouqyd50h/JZ0/9s6x1
xC60XeHgEWe+o1TRTS6FqN40Pn6+ijSo/Mvf1+9wZfBdI6rY4424qX/SeVbir8B1
uZ6MLnXRT+Ssn5f3xEJEjoV9sCpkrCEwGyYRFEpGy4ANyOh57TDihfA1zM8Su1xs
ThCJLOPH
=BFqQ
-----END PGP SIGNATURE-----
Timestamp of file with hash 7d5daa2b5604084f8abd3f86e9acbccc87bc5f25ecf25d02fa93428c29c4752d -
</details>