summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-01-08 03:12:18 +0000
committerkosak@google.com <kosak@google.com@861a406c-534a-0410-8894-cb66d6ee9925>2015-01-08 03:12:18 +0000
commitad17d499bf1211a8cb808a8710f11237c78d5761 (patch)
tree9678b3dc4beeac32a214a260aa63817bed181521
parent95afefe85311353fd4b7350091c4b8dfb62b8c48 (diff)
downloadgoogletest-ad17d499bf1211a8cb808a8710f11237c78d5761.tar.gz
Small Mingw localtime() fix.
Thanks tberghammer for pointing it out. https://codereview.appspot.com/185420043/ git-svn-id: http://googletest.googlecode.com/svn/trunk@702 861a406c-534a-0410-8894-cb66d6ee9925
-rw-r--r--src/gtest.cc27
1 files changed, 18 insertions, 9 deletions
diff --git a/src/gtest.cc b/src/gtest.cc
index 64ab767..7fd5f29 100644
--- a/src/gtest.cc
+++ b/src/gtest.cc
@@ -3505,19 +3505,28 @@ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) {
return ss.str();
}
+static bool PortableLocaltime(time_t seconds, struct tm* out) {
+#if defined(_MSC_VER)
+ return localtime_s(out, &seconds) == 0;
+#elif defined(__MINGW32__) || defined(__MINGW64__)
+ // MINGW <time.h> provides neither localtime_r nor localtime_s, but uses
+ // Windows' localtime(), which has a thread-local tm buffer.
+ struct tm* tm_ptr = localtime(&seconds); // NOLINT
+ if (tm_ptr == NULL)
+ return false;
+ *out = *tm_ptr;
+ return true;
+#else
+ return localtime_r(&seconds, out) != NULL;
+#endif
+}
+
// Converts the given epoch time in milliseconds to a date string in the ISO
// 8601 format, without the timezone information.
std::string FormatEpochTimeInMillisAsIso8601(TimeInMillis ms) {
- time_t seconds = static_cast<time_t>(ms / 1000);
struct tm time_struct;
-#ifdef _MSC_VER
- if (localtime_s(&time_struct, &seconds) != 0)
- return ""; // Invalid ms value
-#else
- if (localtime_r(&seconds, &time_struct) == NULL)
- return ""; // Invalid ms value
-#endif
-
+ if (!PortableLocaltime(static_cast<time_t>(ms / 1000), &time_struct))
+ return "";
// YYYY-MM-DDThh:mm:ss
return StreamableToString(time_struct.tm_year + 1900) + "-" +
String::FormatIntWidth2(time_struct.tm_mon + 1) + "-" +