aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2018-04-14 18:32:43 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2018-04-14 18:32:55 +0200
commitdec61152d6cc711d627d5dee497156d4a19c2499 (patch)
tree2c90dddf1bea5f4de63490b1f6c8b55603725dd8 /src
parent5f2a39946fd42535038e0143cbd289d3070b9f07 (diff)
parent152701568159915828613fa131e94e21337c0c2d (diff)
downloadbitcoin-dec61152d6cc711d627d5dee497156d4a19c2499.tar.xz
Merge #12973: Avoid std::locale/imbue madness in DateTimeStrFormat
1527015 Avoid std::locale/imbue in DateTimeStrFormat (Pieter Wuille) Pull request description: And replace them with just hardcoded ISO8601 strings and `gmtime_r`. Pointed out by @laanwj here: https://github.com/bitcoin/bitcoin/pull/12970#issuecomment-380962488 Tree-SHA512: a459758b42ca56f8462115aefe8e6377c1319fce509ea64dbb767f3f087c9b848335954cb684e5896c38008847684045505a3e1559fb3e83b8e80e10b003d1e7
Diffstat (limited to 'src')
-rw-r--r--src/test/util_tests.cpp11
-rw-r--r--src/utiltime.cpp29
-rw-r--r--src/utiltime.h2
3 files changed, 14 insertions, 28 deletions
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index d41c43a795..e8c7b80c3a 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -159,17 +159,6 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
}
-BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
-{
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0), "1970-01-01 00:00:00");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 0x7FFFFFFF), "2038-01-19 03:14:07");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", 1317425777), "2011-09-30T23:36:17Z");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%H:%M:%SZ", 1317425777), "23:36:17Z");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M", 1317425777), "2011-09-30 23:36");
- BOOST_CHECK_EQUAL(DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", 1317425777), "Fri, 30 Sep 2011 23:36:17 +0000");
-}
-
BOOST_AUTO_TEST_CASE(util_FormatISO8601DateTime)
{
BOOST_CHECK_EQUAL(FormatISO8601DateTime(1317425777), "2011-09-30T23:36:17Z");
diff --git a/src/utiltime.cpp b/src/utiltime.cpp
index 8a861039b3..34800c7b6d 100644
--- a/src/utiltime.cpp
+++ b/src/utiltime.cpp
@@ -10,9 +10,10 @@
#include <utiltime.h>
#include <atomic>
-
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
+#include <ctime>
+#include <tinyformat.h>
static std::atomic<int64_t> nMockTime(0); //!< For unit testing
@@ -75,25 +76,23 @@ void MilliSleep(int64_t n)
#endif
}
-std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
-{
- static std::locale classic(std::locale::classic());
- // std::locale takes ownership of the pointer
- std::locale loc(classic, new boost::posix_time::time_facet(pszFormat));
- std::stringstream ss;
- ss.imbue(loc);
- ss << boost::posix_time::from_time_t(nTime);
- return ss.str();
-}
-
std::string FormatISO8601DateTime(int64_t nTime) {
- return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime);
+ struct tm ts;
+ time_t time_val = nTime;
+ gmtime_r(&time_val, &ts);
+ return strprintf("%04i-%02i-%02iT%02i:%02i:%02iZ", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday, ts.tm_hour, ts.tm_min, ts.tm_sec);
}
std::string FormatISO8601Date(int64_t nTime) {
- return DateTimeStrFormat("%Y-%m-%d", nTime);
+ struct tm ts;
+ time_t time_val = nTime;
+ gmtime_r(&time_val, &ts);
+ return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday);
}
std::string FormatISO8601Time(int64_t nTime) {
- return DateTimeStrFormat("%H:%M:%SZ", nTime);
+ struct tm ts;
+ time_t time_val = nTime;
+ gmtime_r(&time_val, &ts);
+ return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec);
}
diff --git a/src/utiltime.h b/src/utiltime.h
index 807c52ffaf..3bcbb00c16 100644
--- a/src/utiltime.h
+++ b/src/utiltime.h
@@ -31,8 +31,6 @@ void MilliSleep(int64_t n);
* ISO 8601 formatting is preferred. Use the FormatISO8601{DateTime,Date,Time}
* helper functions if possible.
*/
-std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);
-
std::string FormatISO8601DateTime(int64_t nTime);
std::string FormatISO8601Date(int64_t nTime);
std::string FormatISO8601Time(int64_t nTime);