From 47561360904677415ce7aaea68ecc3a1148d32f6 Mon Sep 17 00:00:00 2001 From: Robert Fancsik Date: Wed, 17 Feb 2021 15:25:42 +0100 Subject: [PATCH] Keep GNU fastpath for jerry_port_get_local_time_zone_adjustment PR #4513 caused a measurable slowdown by removing the GNU specific TZA calculation. This patch reverts this removal but also keeps the general implementation introduced in #4513 JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik robert.fancsik@h-lab.eu --- jerry-port/default/CMakeLists.txt | 16 ++++++++++++++++ jerry-port/default/default-date.c | 18 +++++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/jerry-port/default/CMakeLists.txt b/jerry-port/default/CMakeLists.txt index f70ff24c3b..f7038e2dab 100644 --- a/jerry-port/default/CMakeLists.txt +++ b/jerry-port/default/CMakeLists.txt @@ -59,6 +59,22 @@ endif() # (should only be necessary if we used compiler default libc but not checking that) set(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE) +INCLUDE (CheckStructHasMember) +# CHECK_STRUCT_HAS_MEMBER works by trying to compile some C code that accesses the +# given field of the given struct. However, our default compiler options break this +# C code, so turn a couple of them off for this. +if(USING_GCC OR USING_CLANG) + set(CMAKE_REQUIRED_FLAGS "-Wno-error=strict-prototypes -Wno-error=old-style-definition -Wno-error=unused-value") +endif() +# tm.tm_gmtoff is non-standard, so glibc doesn't expose it in c99 mode +# (our default). Define some macros to expose it anyway. +set(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE -D_DEFAULT_SOURCE") +CHECK_STRUCT_HAS_MEMBER ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF) +# localtime_r is is not threadsafe with clang on OSX +if(HAVE_TM_GMTOFF AND NOT "${PLATFORM}" STREQUAL "DARWIN" AND NOT USING_CLANG) + set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TM_GMTOFF) +endif() + # Default Jerry port implementation library add_library(${JERRY_PORT_DEFAULT_NAME} ${SOURCE_PORT_DEFAULT}) target_include_directories(${JERRY_PORT_DEFAULT_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT}) diff --git a/jerry-port/default/default-date.c b/jerry-port/default/default-date.c index 7ff3f5d7db..77baec7592 100644 --- a/jerry-port/default/default-date.c +++ b/jerry-port/default/default-date.c @@ -59,7 +59,19 @@ static double FileTimeToUnixTimeMs (FILETIME ft) double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */ bool is_utc) /**< is the time above in UTC? */ { -#ifdef _WIN32 +#if defined (HAVE_TM_GMTOFF) + struct tm tm; + time_t now = (time_t) (unix_ms / 1000); + localtime_r (&now, &tm); + + if (!is_utc) + { + now -= tm.tm_gmtoff; + localtime_r (&now, &tm); + } + + return ((double) tm.tm_gmtoff) * 1000; +#elif defined (_WIN32) FILETIME fileTime, localFileTime; SYSTEMTIME systemTime, localSystemTime; ULARGE_INTEGER time, localTime; @@ -111,11 +123,11 @@ double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since } return tza_s * 1000; -#else /* !_WIN32 && !__GNUC__ && !__clang__ */ +#else /* !HAVE_TM_GMTOFF && !_WIN32 && !__GNUC__ && !__clang__ */ (void) unix_ms; /* unused */ (void) is_utc; /* unused */ return 0.0; -#endif /* _WIN32 */ +#endif /* HAVE_TM_GMTOFF */ } /* jerry_port_get_local_time_zone_adjustment */ /**