Skip to content

Commit c14702c

Browse files
author
Robert Fancsik
authored
Keep GNU fastpath for jerry_port_get_local_time_zone_adjustment (#4600)
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 [email protected]
1 parent 112ad83 commit c14702c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

jerry-port/default/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ endif()
5959
# (should only be necessary if we used compiler default libc but not checking that)
6060
set(DEFINES_PORT_DEFAULT _BSD_SOURCE _DEFAULT_SOURCE)
6161

62+
INCLUDE (CheckStructHasMember)
63+
# CHECK_STRUCT_HAS_MEMBER works by trying to compile some C code that accesses the
64+
# given field of the given struct. However, our default compiler options break this
65+
# C code, so turn a couple of them off for this.
66+
if(USING_GCC OR USING_CLANG)
67+
set(CMAKE_REQUIRED_FLAGS "-Wno-error=strict-prototypes -Wno-error=old-style-definition -Wno-error=unused-value")
68+
endif()
69+
# tm.tm_gmtoff is non-standard, so glibc doesn't expose it in c99 mode
70+
# (our default). Define some macros to expose it anyway.
71+
set(CMAKE_REQUIRED_DEFINITIONS "-D_BSD_SOURCE -D_DEFAULT_SOURCE")
72+
CHECK_STRUCT_HAS_MEMBER ("struct tm" tm_gmtoff time.h HAVE_TM_GMTOFF)
73+
# localtime_r is is not threadsafe with clang on OSX
74+
if(HAVE_TM_GMTOFF AND NOT "${PLATFORM}" STREQUAL "DARWIN" AND NOT USING_CLANG)
75+
set(DEFINES_PORT_DEFAULT ${DEFINES_PORT_DEFAULT} HAVE_TM_GMTOFF)
76+
endif()
77+
6278
# Default Jerry port implementation library
6379
add_library(${JERRY_PORT_DEFAULT_NAME} ${SOURCE_PORT_DEFAULT})
6480
target_include_directories(${JERRY_PORT_DEFAULT_NAME} PUBLIC ${INCLUDE_PORT_DEFAULT})

jerry-port/default/default-date.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,19 @@ static double FileTimeToUnixTimeMs (FILETIME ft)
5959
double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since unix epoch */
6060
bool is_utc) /**< is the time above in UTC? */
6161
{
62-
#ifdef _WIN32
62+
#if defined (HAVE_TM_GMTOFF)
63+
struct tm tm;
64+
time_t now = (time_t) (unix_ms / 1000);
65+
localtime_r (&now, &tm);
66+
67+
if (!is_utc)
68+
{
69+
now -= tm.tm_gmtoff;
70+
localtime_r (&now, &tm);
71+
}
72+
73+
return ((double) tm.tm_gmtoff) * 1000;
74+
#elif defined (_WIN32)
6375
FILETIME fileTime, localFileTime;
6476
SYSTEMTIME systemTime, localSystemTime;
6577
ULARGE_INTEGER time, localTime;
@@ -111,11 +123,11 @@ double jerry_port_get_local_time_zone_adjustment (double unix_ms, /**< ms since
111123
}
112124

113125
return tza_s * 1000;
114-
#else /* !_WIN32 && !__GNUC__ && !__clang__ */
126+
#else /* !HAVE_TM_GMTOFF && !_WIN32 && !__GNUC__ && !__clang__ */
115127
(void) unix_ms; /* unused */
116128
(void) is_utc; /* unused */
117129
return 0.0;
118-
#endif /* _WIN32 */
130+
#endif /* HAVE_TM_GMTOFF */
119131
} /* jerry_port_get_local_time_zone_adjustment */
120132

121133
/**

0 commit comments

Comments
 (0)