From b0bf038605dc43474bdd9105a5f7162c57ad8bad Mon Sep 17 00:00:00 2001 From: svost Date: Wed, 18 Mar 2015 17:15:52 +0300 Subject: [PATCH] Replace non-threadsafe gmtime and setlocale --- src/bitcoinrpc.cpp | 10 +--------- src/util.cpp | 10 ++++++++++ src/util.h | 9 +-------- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index d944b92..03bd10e 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -361,15 +361,7 @@ string HTTPPost(const string& strMsg, const map& mapRequestHeader 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()); } static string HTTPReply(int nStatus, const string& strMsg, bool keepalive) diff --git a/src/util.cpp b/src/util.cpp index 481da94..8b0afa9 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -1464,3 +1464,13 @@ bool NewThread(void(*pfn)(void*), void* parg) } return true; } + +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(); +} \ No newline at end of file diff --git a/src/util.h b/src/util.h index 77b439e..53dc567 100644 --- a/src/util.h +++ b/src/util.h @@ -351,14 +351,7 @@ inline int64_t GetTimeMillis() boost::posix_time::ptime(boost::gregorian::date(1970,1,1))).total_milliseconds(); } -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); static const std::string strTimestampFormat = "%Y-%m-%d %H:%M:%S UTC"; inline std::string DateTimeStrFormat(int64_t nTime) -- 1.7.1