From 7f23a1e1c1484478590430949988d9a61c6fbb25 Mon Sep 17 00:00:00 2001 From: Gaspard Petit Date: Mon, 30 May 2022 12:45:06 -0400 Subject: [PATCH] Move the LocalTime implementation to gtest-port.h Move the LocalTime implementation to gtest-port.h so it can be customized for each port --- .../include/gtest/internal/gtest-port.h | 28 +++++++++++++++++++ googletest/src/gtest.cc | 17 +---------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/googletest/include/gtest/internal/gtest-port.h b/googletest/include/gtest/internal/gtest-port.h index 7162e6e18..9141ea397 100644 --- a/googletest/include/gtest/internal/gtest-port.h +++ b/googletest/include/gtest/internal/gtest-port.h @@ -2405,4 +2405,32 @@ using Variant = ::std::variant; #endif // __has_include #endif // GTEST_HAS_ABSL +#ifndef GTEST_INTERNAL_LOCALTIME_ +#include // NOLINT +namespace testing { +namespace internal { +inline bool LocalTime(time_t seconds, struct tm* out) { +#if defined(_MSC_VER) + return localtime_s(out, &seconds) == 0; +#elif defined(__MINGW32__) || defined(__MINGW64__) + // MINGW 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 == nullptr) return false; + *out = *tm_ptr; + return true; +#elif defined(__STDC_LIB_EXT1__) + // Uses localtime_s when available as localtime_r is only available from + // C23 standard. + return localtime_s(&seconds, out) != nullptr; +#else + return localtime_r(&seconds, out) != nullptr; +#endif +} +} // namespace internal +} // namespace testing +#endif // GTEST_INTERNAL_LOCALTIME_ + + + #endif // GOOGLETEST_INCLUDE_GTEST_INTERNAL_GTEST_PORT_H_ diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc index 1acad59a7..6c0e58662 100644 --- a/googletest/src/gtest.cc +++ b/googletest/src/gtest.cc @@ -4103,22 +4103,7 @@ std::string FormatTimeInMillisAsSeconds(TimeInMillis ms) { } 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 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 == nullptr) return false; - *out = *tm_ptr; - return true; -#elif defined(__STDC_LIB_EXT1__) - // Uses localtime_s when available as localtime_r is only available from - // C23 standard. - return localtime_s(&seconds, out) != nullptr; -#else - return localtime_r(&seconds, out) != nullptr; -#endif + return internal::LocalTime(seconds, out); } // Converts the given epoch time in milliseconds to a date string in the ISO