From cc8aa7d2d6ec0cb9d0b1b0ff7b9d2d2094e51eb1 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 03:12:59 -0800 Subject: [PATCH 01/19] Fix `getTimezoneOffset()` when `tm_gmtoff` is not available In my environment, the build fails because `tm_gmtoff` is not available: ``` quickjs.c: In function 'getTimezoneOffset': quickjs.c:40527:15: error: 'struct tm' has no member named 'tm_gmtoff' 40527 | return -tm.tm_gmtoff / 60; | ^ ``` This approach attempts to reimplement the `getTimezoneOffset()` to not rely on that property. This change works for my build environment, but I am open to any suggestions for improvement. I didn't test, but this might also work on Windows where this logic is currently not implemented. --- quickjs.c | 54 +++++++++++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) diff --git a/quickjs.c b/quickjs.c index 0c02b5675..cb507d6a0 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40493,38 +40493,31 @@ static const JSCFunctionListEntry js_math_obj[] = { /* OS dependent. d = argv[0] is in ms from 1970. Return the difference between UTC time and local time 'd' in minutes */ -static int getTimezoneOffset(int64_t time) { +static int getTimezoneOffset() { #if defined(_WIN32) /* XXX: TODO */ return 0; #else - time_t ti; - struct tm tm; - - time /= 1000; /* convert to seconds */ - if (sizeof(time_t) == 4) { - /* on 32-bit systems, we need to clamp the time value to the - range of `time_t`. This is better than truncating values to - 32 bits and hopefully provides the same result as 64-bit - implementation of localtime_r. - */ - if ((time_t)-1 < 0) { - if (time < INT32_MIN) { - time = INT32_MIN; - } else if (time > INT32_MAX) { - time = INT32_MAX; - } - } else { - if (time < 0) { - time = 0; - } else if (time > UINT32_MAX) { - time = UINT32_MAX; - } - } + time_t unixTime = time(NULL); + + struct tm* localTime = localtime(&unixTime); + time_t localTimeNum = mktime(localTime); + + struct tm *gmtTime = gmtime(&unixTime); + time_t gmtTimeNum = mktime(gmtTime); + + // Calculate the time zone offset in minutes + int timeZoneOffset = (int)difftime(localTimeNum, gmtTimeNum) / 60; + + // In JavaScript, it's the opposite + timeZoneOffset = -timeZoneOffset; + + // Take off another hour if daylight savings it active + if (localTime->tm_isdst) { + timeZoneOffset = timeZoneOffset - 60; } - ti = time; - localtime_r(&ti, &tm); - return -tm.tm_gmtoff / 60; + + return timeZoneOffset; #endif } @@ -46531,7 +46524,7 @@ static __exception int get_date_fields(JSContext *ctx, JSValue obj, } else { d = dval; if (is_local) { - tz = -getTimezoneOffset(d); + tz = -getTimezoneOffset(); d += tz * 60000; } } @@ -46600,7 +46593,7 @@ static double set_date_fields(double fields[], int is_local) { fields[5] * 1000 + fields[6]; d = days * 86400000 + h; if (is_local) - d += getTimezoneOffset(d) * 60000; + d += getTimezoneOffset() * 60000; return time_clip(d); } @@ -47203,7 +47196,6 @@ static JSValue js_date_Symbol_toPrimitive(JSContext *ctx, JSValue this_val, static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { - // getTimezoneOffset() double v; if (JS_ThisTimeValue(ctx, &v, this_val)) @@ -47211,7 +47203,7 @@ static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val, if (isnan(v)) return JS_NAN; else - return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v))); + return JS_NewInt64(ctx, getTimezoneOffset()); } static JSValue js_date_getTime(JSContext *ctx, JSValue this_val, From 181932980944fe446b0a165170830a40c7cb9ced Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 17:00:01 -0800 Subject: [PATCH 02/19] Simplify --- quickjs.c | 35 ++++++----------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/quickjs.c b/quickjs.c index cb507d6a0..7321104d1 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40493,32 +40493,9 @@ static const JSCFunctionListEntry js_math_obj[] = { /* OS dependent. d = argv[0] is in ms from 1970. Return the difference between UTC time and local time 'd' in minutes */ -static int getTimezoneOffset() { -#if defined(_WIN32) - /* XXX: TODO */ - return 0; -#else - time_t unixTime = time(NULL); - - struct tm* localTime = localtime(&unixTime); - time_t localTimeNum = mktime(localTime); - - struct tm *gmtTime = gmtime(&unixTime); - time_t gmtTimeNum = mktime(gmtTime); - - // Calculate the time zone offset in minutes - int timeZoneOffset = (int)difftime(localTimeNum, gmtTimeNum) / 60; - - // In JavaScript, it's the opposite - timeZoneOffset = -timeZoneOffset; - - // Take off another hour if daylight savings it active - if (localTime->tm_isdst) { - timeZoneOffset = timeZoneOffset - 60; - } - - return timeZoneOffset; -#endif +static int getTimezoneOffset(int64_t time) { + struct tm *gmt = gmtime((const time_t *)&time); + return (mktime(gmt) - time) / 60; } /* RegExp */ @@ -46524,7 +46501,7 @@ static __exception int get_date_fields(JSContext *ctx, JSValue obj, } else { d = dval; if (is_local) { - tz = -getTimezoneOffset(); + tz = -getTimezoneOffset(d); d += tz * 60000; } } @@ -46593,7 +46570,7 @@ static double set_date_fields(double fields[], int is_local) { fields[5] * 1000 + fields[6]; d = days * 86400000 + h; if (is_local) - d += getTimezoneOffset() * 60000; + d += getTimezoneOffset(d) * 60000; return time_clip(d); } @@ -47203,7 +47180,7 @@ static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val, if (isnan(v)) return JS_NAN; else - return JS_NewInt64(ctx, getTimezoneOffset()); + return JS_NewInt64(ctx, getTimezoneOffset((int64_t)trunc(v))); } static JSValue js_date_getTime(JSContext *ctx, JSValue this_val, From fe049ff5b84d3c06bf6a6d2ff497a6f62db67404 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 17:17:03 -0800 Subject: [PATCH 03/19] Add timezone adjustment --- quickjs.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index 7321104d1..6d916f218 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40494,8 +40494,19 @@ static const JSCFunctionListEntry js_math_obj[] = { /* OS dependent. d = argv[0] is in ms from 1970. Return the difference between UTC time and local time 'd' in minutes */ static int getTimezoneOffset(int64_t time) { - struct tm *gmt = gmtime((const time_t *)&time); - return (mktime(gmt) - time) / 60; + struct tm local; + struct tm gmt; + time /= 1000; /* convert to seconds */ + localtime_r((const time_t *)&time, &local); + gmtime_r((const time_t *)&time, &gmt); + int offset = (mktime(&gmt) - time) / 60; + + /* take off another hour if daylight savings is active */ + if (local.tm_isdst) { + offset -= 60; + } + + return offset; } /* RegExp */ From 8ad0bb8ea2571c340f6414c11efbda08a4c76c4d Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 17:25:20 -0800 Subject: [PATCH 04/19] Minimize diff --- quickjs.c | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/quickjs.c b/quickjs.c index 6d916f218..27b5b9c44 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40494,12 +40494,38 @@ static const JSCFunctionListEntry js_math_obj[] = { /* OS dependent. d = argv[0] is in ms from 1970. Return the difference between UTC time and local time 'd' in minutes */ static int getTimezoneOffset(int64_t time) { - struct tm local; +#if defined(_WIN32) + /* XXX: TODO */ + return 0; +#else + time_t ti; struct tm gmt; + struct tm local; time /= 1000; /* convert to seconds */ - localtime_r((const time_t *)&time, &local); - gmtime_r((const time_t *)&time, &gmt); - int offset = (mktime(&gmt) - time) / 60; + if (sizeof(time_t) == 4) { + /* on 32-bit systems, we need to clamp the time value to the + range of `time_t`. This is better than truncating values to + 32 bits and hopefully provides the same result as 64-bit + implementation of localtime_r. + */ + if ((time_t)-1 < 0) { + if (time < INT32_MIN) { + time = INT32_MIN; + } else if (time > INT32_MAX) { + time = INT32_MAX; + } + } else { + if (time < 0) { + time = 0; + } else if (time > UINT32_MAX) { + time = UINT32_MAX; + } + } + } + ti = time; + localtime_r(&ti, &local); + gmtime_r(&ti, &gmt); + int offset = (mktime(&gmt) - ti) / 60; /* take off another hour if daylight savings is active */ if (local.tm_isdst) { @@ -40507,6 +40533,7 @@ static int getTimezoneOffset(int64_t time) { } return offset; +#endif } /* RegExp */ From 2871afbd423eddcb5ae1934c10d472589b4417a6 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 17:59:22 -0800 Subject: [PATCH 05/19] . --- quickjs.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/quickjs.c b/quickjs.c index 27b5b9c44..b4d2bf07b 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40499,8 +40499,8 @@ static int getTimezoneOffset(int64_t time) { return 0; #else time_t ti; + struct tm tm; struct tm gmt; - struct tm local; time /= 1000; /* convert to seconds */ if (sizeof(time_t) == 4) { /* on 32-bit systems, we need to clamp the time value to the @@ -40523,12 +40523,12 @@ static int getTimezoneOffset(int64_t time) { } } ti = time; - localtime_r(&ti, &local); + localtime_r(&ti, &tm); gmtime_r(&ti, &gmt); int offset = (mktime(&gmt) - ti) / 60; /* take off another hour if daylight savings is active */ - if (local.tm_isdst) { + if (tm.tm_isdst) { offset -= 60; } From 016903800f12cb8b36f5f2c77f8be704a71e0968 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Mon, 18 Dec 2023 18:00:02 -0800 Subject: [PATCH 06/19] . --- quickjs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/quickjs.c b/quickjs.c index b4d2bf07b..9de928e78 100644 --- a/quickjs.c +++ b/quickjs.c @@ -47211,6 +47211,7 @@ static JSValue js_date_Symbol_toPrimitive(JSContext *ctx, JSValue this_val, static JSValue js_date_getTimezoneOffset(JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { + // getTimezoneOffset() double v; if (JS_ThisTimeValue(ctx, &v, this_val)) From 162082c4af43b8fa2d2fd090735c9c082a1993b3 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 20 Dec 2023 15:42:32 -0800 Subject: [PATCH 07/19] Remove manual dst adjustment --- quickjs.c | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/quickjs.c b/quickjs.c index 9de928e78..7ff8fdb8c 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40525,14 +40525,11 @@ static int getTimezoneOffset(int64_t time) { ti = time; localtime_r(&ti, &tm); gmtime_r(&ti, &gmt); - int offset = (mktime(&gmt) - ti) / 60; - /* take off another hour if daylight savings is active */ - if (tm.tm_isdst) { - offset -= 60; - } + /* adjust the gmt struct to represent local time */ + gmt.tm_isdst = tm.tm_isdst; - return offset; + return difftime(mktime(&gmt), mktime(&tm)) / 60; #endif } From 978a145cf2832008b52d068e09d86c478fb231d5 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 20 Dec 2023 16:05:55 -0800 Subject: [PATCH 08/19] Use CMake to check if `tm_gmtoff` exists --- .gitignore | 1 + CMakeLists.txt | 4 ++++ config.h.in | 1 + quickjs.c | 7 ++++++- 4 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 config.h.in diff --git a/.gitignore b/.gitignore index a1aae7192..989526fc2 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ build/ unicode/ test262_*.txt +config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2d7eaf5f8..b0ee3e23b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 3.9) project(quickjs LANGUAGES C) include(CheckCCompilerFlag) +include(CheckStructHasMember) include(GNUInstallDirs) # TODO: @@ -63,6 +64,9 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") xcheck_add_c_compiler_flag(-fno-omit-frame-pointer) endif() +check_struct_has_member("struct tm" tm_gmtoff "time.h" HAVE_TM_GMTOFF LANGUAGE C) +configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h) + macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT) option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT}) if(DEFINED ENV{${OPTION_NAME}}) diff --git a/config.h.in b/config.h.in new file mode 100644 index 000000000..771cda44c --- /dev/null +++ b/config.h.in @@ -0,0 +1 @@ +#cmakedefine HAVE_TM_GMTOFF diff --git a/quickjs.c b/quickjs.c index 7ff8fdb8c..8a7248f37 100644 --- a/quickjs.c +++ b/quickjs.c @@ -42,6 +42,7 @@ #include "quickjs.h" #include "libregexp.h" #include "libbf.h" +#include "config.h" #if defined(EMSCRIPTEN) || defined(_MSC_VER) #define DIRECT_DISPATCH 0 @@ -40500,7 +40501,6 @@ static int getTimezoneOffset(int64_t time) { #else time_t ti; struct tm tm; - struct tm gmt; time /= 1000; /* convert to seconds */ if (sizeof(time_t) == 4) { /* on 32-bit systems, we need to clamp the time value to the @@ -40524,12 +40524,17 @@ static int getTimezoneOffset(int64_t time) { } ti = time; localtime_r(&ti, &tm); +#ifdef HAVE_TM_GMTOFF + return -tm.tm_gmtoff / 60; +#else + struct tm gmt; gmtime_r(&ti, &gmt); /* adjust the gmt struct to represent local time */ gmt.tm_isdst = tm.tm_isdst; return difftime(mktime(&gmt), mktime(&tm)) / 60; +#endif /* HAVE_TM_GMTOFF */ #endif } From e397c3671a0aa20055146ab78e5a11277f0c9478 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 20 Dec 2023 16:20:36 -0800 Subject: [PATCH 09/19] . --- quickjs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/quickjs.c b/quickjs.c index 8a7248f37..3ee8aee89 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40501,6 +40501,7 @@ static int getTimezoneOffset(int64_t time) { #else time_t ti; struct tm tm; + time /= 1000; /* convert to seconds */ if (sizeof(time_t) == 4) { /* on 32-bit systems, we need to clamp the time value to the From c257fc0e004833c95b727759a1d282ba85bff82b Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 Dec 2023 01:24:17 -0800 Subject: [PATCH 10/19] Switch to -D define instead of `config.h` --- .gitignore | 1 - CMakeLists.txt | 4 +++- config.h.in | 1 - quickjs.c | 1 - 4 files changed, 3 insertions(+), 4 deletions(-) delete mode 100644 config.h.in diff --git a/.gitignore b/.gitignore index 989526fc2..a1aae7192 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,3 @@ build/ unicode/ test262_*.txt -config.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b0ee3e23b..e62a142dc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -65,7 +65,9 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") endif() check_struct_has_member("struct tm" tm_gmtoff "time.h" HAVE_TM_GMTOFF LANGUAGE C) -configure_file(${CMAKE_CURRENT_SOURCE_DIR}/config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/config.h) +if(HAVE_TM_GMTOFF) + add_compile_definitions(HAVE_TM_GMTOFF) +endif() macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT) option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT}) diff --git a/config.h.in b/config.h.in deleted file mode 100644 index 771cda44c..000000000 --- a/config.h.in +++ /dev/null @@ -1 +0,0 @@ -#cmakedefine HAVE_TM_GMTOFF diff --git a/quickjs.c b/quickjs.c index 3ee8aee89..0b03affa3 100644 --- a/quickjs.c +++ b/quickjs.c @@ -42,7 +42,6 @@ #include "quickjs.h" #include "libregexp.h" #include "libbf.h" -#include "config.h" #if defined(EMSCRIPTEN) || defined(_MSC_VER) #define DIRECT_DISPATCH 0 From 9bb0a1551c5bc6f25348493d110f15c1cb32d843 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Thu, 21 Dec 2023 02:24:53 -0800 Subject: [PATCH 11/19] Disable DST on local struct --- quickjs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index 0b03affa3..2eeb9cf8c 100644 --- a/quickjs.c +++ b/quickjs.c @@ -40530,8 +40530,8 @@ static int getTimezoneOffset(int64_t time) { struct tm gmt; gmtime_r(&ti, &gmt); - /* adjust the gmt struct to represent local time */ - gmt.tm_isdst = tm.tm_isdst; + /* disable DST adjustment on the local tm struct */ + tm.tm_isdst = 0; return difftime(mktime(&gmt), mktime(&tm)) / 60; #endif /* HAVE_TM_GMTOFF */ From cb0e4ffe6768b39fbeabd2c74a79ef4d92d9f774 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 00:41:14 -0800 Subject: [PATCH 12/19] Remove auto-detection, use explicit flag and enable for newlib by default --- CMakeLists.txt | 10 +++++----- quickjs.c | 14 ++++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e62a142dc..37ce67122 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,11 +64,6 @@ if(CMAKE_BUILD_TYPE MATCHES "Debug") xcheck_add_c_compiler_flag(-fno-omit-frame-pointer) endif() -check_struct_has_member("struct tm" tm_gmtoff "time.h" HAVE_TM_GMTOFF LANGUAGE C) -if(HAVE_TM_GMTOFF) - add_compile_definitions(HAVE_TM_GMTOFF) -endif() - macro(xoption OPTION_NAME OPTION_TEXT OPTION_DEFAULT) option(${OPTION_NAME} ${OPTION_TEXT} ${OPTION_DEFAULT}) if(DEFINED ENV{${OPTION_NAME}}) @@ -91,6 +86,11 @@ xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) +xoption(CONFIG_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm` " ON) + +if(NOT CONFIG_TM_GMTOFF) + add_compile_definitions(NO_TM_GMTOFF) +endif() if(CONFIG_ASAN) message(STATUS "Building with ASan") diff --git a/quickjs.c b/quickjs.c index 2eeb9cf8c..3fa8a1c7d 100644 --- a/quickjs.c +++ b/quickjs.c @@ -60,6 +60,12 @@ #define CONFIG_PRINTF_RNDN #endif +#if defined(_NEWLIB_STDIO_H) +/* define if `struct tm` does not contain `tm_gmtoff` property */ +/* this can also be enabled through `cmake -DCONFIG_TM_GMTOFF=OFF */ +#define NO_TM_GMTOFF +#endif + /* dump object free */ //#define DUMP_FREE //#define DUMP_CLOSURE @@ -40524,9 +40530,7 @@ static int getTimezoneOffset(int64_t time) { } ti = time; localtime_r(&ti, &tm); -#ifdef HAVE_TM_GMTOFF - return -tm.tm_gmtoff / 60; -#else +#ifdef NO_TM_GMTOFF struct tm gmt; gmtime_r(&ti, &gmt); @@ -40534,7 +40538,9 @@ static int getTimezoneOffset(int64_t time) { tm.tm_isdst = 0; return difftime(mktime(&gmt), mktime(&tm)) / 60; -#endif /* HAVE_TM_GMTOFF */ +#else + return -tm.tm_gmtoff / 60; +#endif /* NO_TM_GMTOFF */ #endif } From ebb4d7906d60b40b9a31b73f79b54b6d46c4a24e Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 01:07:16 -0800 Subject: [PATCH 13/19] Check for `__NEWLIB__` --- quickjs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 3fa8a1c7d..78708309c 100644 --- a/quickjs.c +++ b/quickjs.c @@ -60,7 +60,7 @@ #define CONFIG_PRINTF_RNDN #endif -#if defined(_NEWLIB_STDIO_H) +#if defined(__NEWLIB__) /* define if `struct tm` does not contain `tm_gmtoff` property */ /* this can also be enabled through `cmake -DCONFIG_TM_GMTOFF=OFF */ #define NO_TM_GMTOFF From c522d1dfb24693bc2e22c8e7af718bccf42eb589 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 01:14:24 -0800 Subject: [PATCH 14/19] Simplify --- CMakeLists.txt | 6 +----- quickjs.c | 14 +++++++------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 37ce67122..56da92ebd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,11 +86,7 @@ xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) -xoption(CONFIG_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm` " ON) - -if(NOT CONFIG_TM_GMTOFF) - add_compile_definitions(NO_TM_GMTOFF) -endif() +xoption(HAVE_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm` " ON) if(CONFIG_ASAN) message(STATUS "Building with ASan") diff --git a/quickjs.c b/quickjs.c index 78708309c..41e61a994 100644 --- a/quickjs.c +++ b/quickjs.c @@ -61,9 +61,9 @@ #endif #if defined(__NEWLIB__) -/* define if `struct tm` does not contain `tm_gmtoff` property */ -/* this can also be enabled through `cmake -DCONFIG_TM_GMTOFF=OFF */ -#define NO_TM_GMTOFF +/* undefine if `struct tm` does not contain `tm_gmtoff` property */ +/* this can also be configured through `cmake -DHAVE_TM_GMTOFF=OFF */ +#undef HAVE_TM_GMTOFF #endif /* dump object free */ @@ -40530,7 +40530,9 @@ static int getTimezoneOffset(int64_t time) { } ti = time; localtime_r(&ti, &tm); -#ifdef NO_TM_GMTOFF +#ifdef HAVE_TM_GMTOFF + return -tm.tm_gmtoff / 60; +#else struct tm gmt; gmtime_r(&ti, &gmt); @@ -40538,9 +40540,7 @@ static int getTimezoneOffset(int64_t time) { tm.tm_isdst = 0; return difftime(mktime(&gmt), mktime(&tm)) / 60; -#else - return -tm.tm_gmtoff / 60; -#endif /* NO_TM_GMTOFF */ +#endif /* HAVE_TM_GMTOFF */ #endif } From 28614118219f979a598c03d64881319c7c75c161 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 01:15:16 -0800 Subject: [PATCH 15/19] . --- CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 56da92ebd..d7172be64 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,6 @@ cmake_minimum_required(VERSION 3.9) project(quickjs LANGUAGES C) include(CheckCCompilerFlag) -include(CheckStructHasMember) include(GNUInstallDirs) # TODO: From 86e13f234399bcf7819f41c89466b61a8aa89bc8 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 01:17:36 -0800 Subject: [PATCH 16/19] . --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d7172be64..b9bb0b52d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,7 +85,7 @@ xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) -xoption(HAVE_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm` " ON) +xoption(HAVE_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm`" ON) if(CONFIG_ASAN) message(STATUS "Building with ASan") From da5db5e100975dcee7340fd7cda74f5fb060ed38 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 03:29:59 -0800 Subject: [PATCH 17/19] Remove unnecessary comments --- quickjs.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index 41e61a994..8a7259150 100644 --- a/quickjs.c +++ b/quickjs.c @@ -61,8 +61,6 @@ #endif #if defined(__NEWLIB__) -/* undefine if `struct tm` does not contain `tm_gmtoff` property */ -/* this can also be configured through `cmake -DHAVE_TM_GMTOFF=OFF */ #undef HAVE_TM_GMTOFF #endif From 4c4d73dd325935d1c6699a7b1a8024e0c1644e0f Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 23:51:30 -0800 Subject: [PATCH 18/19] Remove CMake option --- CMakeLists.txt | 1 - quickjs.c | 10 +++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b9bb0b52d..2d7eaf5f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -85,7 +85,6 @@ xoption(BUILD_STATIC_QJS_EXE "Build a static qjs executable" OFF) xoption(CONFIG_ASAN "Enable AddressSanitizer (ASan)" OFF) xoption(CONFIG_MSAN "Enable MemorySanitizer (MSan)" OFF) xoption(CONFIG_UBSAN "Enable UndefinedBehaviorSanitizer (UBSan)" OFF) -xoption(HAVE_TM_GMTOFF "Enable when `tm_gmtoff` exists on `struct tm`" ON) if(CONFIG_ASAN) message(STATUS "Building with ASan") diff --git a/quickjs.c b/quickjs.c index 8a7259150..829d5a294 100644 --- a/quickjs.c +++ b/quickjs.c @@ -61,7 +61,7 @@ #endif #if defined(__NEWLIB__) -#undef HAVE_TM_GMTOFF +#undef NO_TM_GMTOFF #endif /* dump object free */ @@ -40528,9 +40528,7 @@ static int getTimezoneOffset(int64_t time) { } ti = time; localtime_r(&ti, &tm); -#ifdef HAVE_TM_GMTOFF - return -tm.tm_gmtoff / 60; -#else +#ifdef NO_TM_GMTOFF struct tm gmt; gmtime_r(&ti, &gmt); @@ -40538,7 +40536,9 @@ static int getTimezoneOffset(int64_t time) { tm.tm_isdst = 0; return difftime(mktime(&gmt), mktime(&tm)) / 60; -#endif /* HAVE_TM_GMTOFF */ +#else + return -tm.tm_gmtoff / 60; +#endif /* NO_TM_GMTOFF */ #endif } From 9cac51cc150c730b79ad1771af34cf89d9fb7cd6 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Sat, 23 Dec 2023 23:53:29 -0800 Subject: [PATCH 19/19] . --- quickjs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 829d5a294..43bb36c97 100644 --- a/quickjs.c +++ b/quickjs.c @@ -61,7 +61,7 @@ #endif #if defined(__NEWLIB__) -#undef NO_TM_GMTOFF +#define NO_TM_GMTOFF #endif /* dump object free */