]> git.kaiwu.me - quickjs.git/commitdiff
more portable and Windows version for getTimezoneOffset() (github issue #122)
authorFabrice Bellard <fabrice@bellard.org>
Thu, 11 Jan 2024 14:26:39 +0000 (15:26 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Thu, 11 Jan 2024 14:26:39 +0000 (15:26 +0100)
quickjs.c

index 51607876d04d048a339e6404eb42d5697e7bc67f..37201fc33a6fda131fa79a6787209602854c529a 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -43165,14 +43165,11 @@ 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) {
-#if defined(_WIN32)
-    /* XXX: TODO */
-    return 0;
-#else
+static int getTimezoneOffset(int64_t time)
+{
     time_t ti;
-    struct tm tm;
-
+    int res;
+    
     time /= 1000; /* convert to seconds */
     if (sizeof(time_t) == 4) {
         /* on 32-bit systems, we need to clamp the time value to the
@@ -43195,9 +43192,27 @@ static int getTimezoneOffset(int64_t time) {
         }
     }
     ti = time;
-    localtime_r(&ti, &tm);
-    return -tm.tm_gmtoff / 60;
+#if defined(_WIN32)
+    {
+        struct tm *tm;
+        time_t gm_ti, loc_ti;
+        
+        tm = gmtime(&ti);
+        gm_ti = mktime(tm);
+        
+        tm = localtime(&ti);
+        loc_ti = mktime(tm);
+
+        res = (gm_ti - loc_ti) / 60;
+    }
+#else
+    {
+        struct tm tm;
+        localtime_r(&ti, &tm);
+        res = -tm.tm_gmtoff / 60;
+    }
 #endif
+    return res;
 }
 
 #if 0