aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWladimir J. van der Laan <laanwj@gmail.com>2014-05-08 18:01:10 +0200
committerWladimir J. van der Laan <laanwj@gmail.com>2014-05-23 16:04:02 +0200
commita62649723bab311107468038e41bf76410990423 (patch)
treecc81cddea34380368b2fb99c005f3dd7ed36fdea
parente0036e9f0e0240541bf5a4ebcd8087be99739e71 (diff)
downloadbitcoin-a62649723bab311107468038e41bf76410990423.tar.xz
Replace non-threadsafe gmtime and setlocale
Make DateTimeStrFormat use boost::posix_time. Also re-enable the util_DateTimeStrFormat tests, as they are no longer platform specific. Rebased-By: Wladimir J. van der Laan <laanwj@gmail.com> Rebased-From: 3e8ac6a
-rw-r--r--src/rpcprotocol.cpp10
-rw-r--r--src/test/util_tests.cpp4
-rw-r--r--src/util.cpp12
-rw-r--r--src/util.h9
4 files changed, 15 insertions, 20 deletions
diff --git a/src/rpcprotocol.cpp b/src/rpcprotocol.cpp
index 5cbaa535ab..2718f81783 100644
--- a/src/rpcprotocol.cpp
+++ b/src/rpcprotocol.cpp
@@ -51,15 +51,7 @@ string HTTPPost(const string& strMsg, const map<string,string>& mapRequestHeader
static string rfc1123Time()
{
- char buffer[64];
- time_t now;
- time(&now);
- struct tm* now_gmt = gmtime(&now);
- string locale(setlocale(LC_TIME, NULL));
- setlocale(LC_TIME, "C"); // we want POSIX (aka "C") weekday/month strings
- strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S +0000", now_gmt);
- setlocale(LC_TIME, locale.c_str());
- return string(buffer);
+ return DateTimeStrFormat("%a, %d %b %Y %H:%M:%S +0000", GetTime());
}
string HTTPReply(int nStatus, const string& strMsg, bool keepalive)
diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp
index 44998cbda7..5155e0c3e5 100644
--- a/src/test/util_tests.cpp
+++ b/src/test/util_tests.cpp
@@ -108,13 +108,11 @@ BOOST_AUTO_TEST_CASE(util_HexStr)
BOOST_AUTO_TEST_CASE(util_DateTimeStrFormat)
{
-/*These are platform-dependant and thus removed to avoid useless test failures
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");
- // Formats used within Bitcoin
BOOST_CHECK_EQUAL(DateTimeStrFormat("%Y-%m-%d %H:%M:%S", 1317425777), "2011-09-30 23:36:17");
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_ParseParameters)
diff --git a/src/util.cpp b/src/util.cpp
index 8af47643cb..b8036a3872 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -14,6 +14,8 @@
#include <stdarg.h>
+#include <boost/date_time/posix_time/posix_time.hpp>
+
#ifndef WIN32
// for posix_fallocate
#ifdef __linux_
@@ -1423,3 +1425,13 @@ void SetupEnvironment()
}
#endif
}
+
+std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
+{
+ // std::locale takes ownership of the pointer
+ std::locale loc(std::locale::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();
+}
diff --git a/src/util.h b/src/util.h
index 5555e1b336..89c6016698 100644
--- a/src/util.h
+++ b/src/util.h
@@ -320,14 +320,7 @@ inline int64_t GetTimeMicros()
boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_microseconds();
}
-inline std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime)
-{
- time_t n = nTime;
- struct tm* ptmTime = gmtime(&n);
- char pszTime[200];
- strftime(pszTime, sizeof(pszTime), pszFormat, ptmTime);
- return pszTime;
-}
+std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime);
template<typename T>
void skipspaces(T& it)