As discussed in #17245.
- Renamed the function.
- Moved it from
rpcdump.cpp
totime.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.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
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);
0int64_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
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+}
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)
FormatParseISO8601DateTime
? I suggest add a separate test case for ParseISO8601DateTime
.
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);
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
Signature:
0-----BEGIN PGP SIGNED MESSAGE-----
1Hash: SHA512
2
3ACK e7b02b54ccfb6b2e119a67799220f8d8d8b5cccd
4-----BEGIN PGP SIGNATURE-----
5
6iQGzBAEBCgAdFiEE+rVPoUahrI9sLGYTzit1aX5ppUgFAlwqrYAACgkQzit1aX5p
7pUjLfgv+N9UU0SzxAKf0FRYvyT/bhaXaiZZ6WgODU8MZffV9imON/+O47MsTOF12
8If2MPrpgSn9KUlqqnfRrYEPkrvC6GG//w63vmZ6JpDgwIQptgZPvddxJF3FPOvay
96sptCfj/ntE+EYnb2Jb3Zw+1FOuSt5le36KbcNlEzSC3eDK71THEZ5/KqwvmCLFD
10CSdVzTCfh3wq+FZdChQUnpzb5E8BKTFjw3oKp9Sph9GJ6DCBZlMEb95LjJnZEx5i
11u3BDMq8TtC2xNDDwm6WllsFhMqWufXwhwUEy+ARK08xd9/HpovrKHax27aSEf/qc
12UG9bf4ITedUh6zKgF9W4C15gnWa9QL/gtyq+bMIC4Rfzl8ouqyd50h/JZ0/9s6x1
13xC60XeHgEWe+o1TRTS6FqN40Pn6+ijSo/Mvf1+9wZfBdI6rY4424qX/SeVbir8B1
14uZ6MLnXRT+Ssn5f3xEJEjoV9sCpkrCEwGyYRFEpGy4ANyOh57TDihfA1zM8Su1xs
15ThCJLOPH
16=BFqQ
17-----END PGP SIGNATURE-----
Timestamp of file with hash 7d5daa2b5604084f8abd3f86e9acbccc87bc5f25ecf25d02fa93428c29c4752d -