Skip to content

Commit cfec3e0

Browse files
author
MarcoFalke
committed
Merge bitcoin#17266: util: Rename DecodeDumpTime to ParseISO8601DateTime
e7b02b5 Add roundtrip and more tests to ParseISO8601DateTime and FormatISO8601DateTime (Elichai Turkel) 9e2c623 Rename DecodeDumpTime to ParseISO8601DateTime and move to time.cpp (Elichai Turkel) Pull request description: As discussed in bitcoin#17245. 1. Renamed the function. 2. Moved it from `rpcdump.cpp` to `time.cpp`. 3. Added a check if the time is less then epoch return 0 to prevent an overflow. 4. Added more edge cases tests and a roundtrip test. ACKs for top commit: laanwj: ACK e7b02b5 MarcoFalke: ACK e7b02b5 promag: Code review ACK e7b02b5. Moved code is correct, left a comment regarding the test change. Tree-SHA512: 703c21e09b2aabc992235149e67acba63d9d77a593ec8f6d2fec3eb63a7e5c406d56cbce6c6513ab32fba43367d073d2345f3b589843e3c5fe4f55ea3e00bf29
2 parents 1ab6c12 + e7b02b5 commit cfec3e0

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

src/test/util_tests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,17 @@ BOOST_AUTO_TEST_CASE(util_Join)
145145
BOOST_CHECK_EQUAL(Join<std::string>({"foo", "bar"}, ", ", op_upper), "FOO, BAR");
146146
}
147147

148-
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
148+
BOOST_AUTO_TEST_CASE(util_FormatParseISO8601DateTime)
149149
{
150150
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
151+
BOOST_CHECK_EQUAL(FormatISO8601DateTime(0), "1970-01-01T00:00:00Z");
152+
153+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1970-01-01T00:00:00Z"), 0);
154+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("1960-01-01T00:00:00Z"), 0);
155+
BOOST_CHECK_EQUAL(ParseISO8601DateTime("2011-09-30T23:36:17Z"), 1317425777);
156+
157+
auto time = GetSystemTimeInSeconds();
158+
BOOST_CHECK_EQUAL(ParseISO8601DateTime(FormatISO8601DateTime(time)), time);
151159
}
152160

153161
BOOST_AUTO_TEST_CASE(util_FormatISO8601Date)

src/util/time.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,17 @@ std::string FormatISO8601Date(int64_t nTime) {
111111
#endif
112112
return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
113113
}
114+
115+
int64_t ParseISO8601DateTime(const std::string& str)
116+
{
117+
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
118+
static const std::locale loc(std::locale::classic(),
119+
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
120+
std::istringstream iss(str);
121+
iss.imbue(loc);
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+
}

src/util/time.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,6 @@ T GetTime();
4848
*/
4949
std::string FormatISO8601DateTime(int64_t nTime);
5050
std::string FormatISO8601Date(int64_t nTime);
51+
int64_t ParseISO8601DateTime(const std::string& str);
5152

5253
#endif // BITCOIN_UTIL_TIME_H

src/wallet/rpcdump.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,23 +23,10 @@
2323
#include <tuple>
2424

2525
#include <boost/algorithm/string.hpp>
26-
#include <boost/date_time/posix_time/posix_time.hpp>
2726

2827
#include <univalue.h>
2928

3029

31-
int64_t static DecodeDumpTime(const std::string &str) {
32-
static const boost::posix_time::ptime epoch = boost::posix_time::from_time_t(0);
33-
static const std::locale loc(std::locale::classic(),
34-
new boost::posix_time::time_input_facet("%Y-%m-%dT%H:%M:%SZ"));
35-
std::istringstream iss(str);
36-
iss.imbue(loc);
37-
boost::posix_time::ptime ptime(boost::date_time::not_a_date_time);
38-
iss >> ptime;
39-
if (ptime.is_not_a_date_time())
40-
return 0;
41-
return (ptime - epoch).total_seconds();
42-
}
4330

4431
std::string static EncodeDumpString(const std::string &str) {
4532
std::stringstream ret;
@@ -598,7 +585,7 @@ UniValue importwallet(const JSONRPCRequest& request)
598585
continue;
599586
CKey key = DecodeSecret(vstr[0]);
600587
if (key.IsValid()) {
601-
int64_t nTime = DecodeDumpTime(vstr[1]);
588+
int64_t nTime = ParseISO8601DateTime(vstr[1]);
602589
std::string strLabel;
603590
bool fLabel = true;
604591
for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) {
@@ -617,7 +604,7 @@ UniValue importwallet(const JSONRPCRequest& request)
617604
} else if(IsHex(vstr[0])) {
618605
std::vector<unsigned char> vData(ParseHex(vstr[0]));
619606
CScript script = CScript(vData.begin(), vData.end());
620-
int64_t birth_time = DecodeDumpTime(vstr[1]);
607+
int64_t birth_time = ParseISO8601DateTime(vstr[1]);
621608
scripts.push_back(std::pair<CScript, int64_t>(script, birth_time));
622609
}
623610
}

0 commit comments

Comments
 (0)