|
10 | 10 | #include <utiltime.h> |
11 | 11 |
|
12 | 12 | #include <atomic> |
13 | | - |
14 | 13 | #include <boost/date_time/posix_time/posix_time.hpp> |
15 | 14 | #include <boost/thread.hpp> |
| 15 | +#include <ctime> |
| 16 | +#include <tinyformat.h> |
16 | 17 |
|
17 | 18 | static std::atomic<int64_t> nMockTime(0); //!< For unit testing |
18 | 19 |
|
@@ -75,25 +76,23 @@ void MilliSleep(int64_t n) |
75 | 76 | #endif |
76 | 77 | } |
77 | 78 |
|
78 | | -std::string DateTimeStrFormat(const char* pszFormat, int64_t nTime) |
79 | | -{ |
80 | | - static std::locale classic(std::locale::classic()); |
81 | | - // std::locale takes ownership of the pointer |
82 | | - std::locale loc(classic, new boost::posix_time::time_facet(pszFormat)); |
83 | | - std::stringstream ss; |
84 | | - ss.imbue(loc); |
85 | | - ss << boost::posix_time::from_time_t(nTime); |
86 | | - return ss.str(); |
87 | | -} |
88 | | - |
89 | 79 | std::string FormatISO8601DateTime(int64_t nTime) { |
90 | | - return DateTimeStrFormat("%Y-%m-%dT%H:%M:%SZ", nTime); |
| 80 | + struct tm ts; |
| 81 | + time_t time_val = nTime; |
| 82 | + gmtime_r(&time_val, &ts); |
| 83 | + 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); |
91 | 84 | } |
92 | 85 |
|
93 | 86 | std::string FormatISO8601Date(int64_t nTime) { |
94 | | - return DateTimeStrFormat("%Y-%m-%d", nTime); |
| 87 | + struct tm ts; |
| 88 | + time_t time_val = nTime; |
| 89 | + gmtime_r(&time_val, &ts); |
| 90 | + return strprintf("%04i-%02i-%02i", ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday); |
95 | 91 | } |
96 | 92 |
|
97 | 93 | std::string FormatISO8601Time(int64_t nTime) { |
98 | | - return DateTimeStrFormat("%H:%M:%SZ", nTime); |
| 94 | + struct tm ts; |
| 95 | + time_t time_val = nTime; |
| 96 | + gmtime_r(&time_val, &ts); |
| 97 | + return strprintf("%02i:%02i:%02iZ", ts.tm_hour, ts.tm_min, ts.tm_sec); |
99 | 98 | } |
0 commit comments