Browse Source

Fixed Win32 timezone offset calculation.

Brucey 2 years ago
parent
commit
75b7926cc9
1 changed files with 27 additions and 19 deletions
  1. 27 19
      stdc.mod/stdc.c

+ 27 - 19
stdc.mod/stdc.c

@@ -926,28 +926,15 @@ typedef struct {
 	int offset;
 	int offset;
 } SDateTime;
 } SDateTime;
 
 
+#ifdef __WIN32__
 int bmx_calc_timeoffset_mins() {
 int bmx_calc_timeoffset_mins() {
-	time_t rawtime;
-    struct tm *local_tm, *utc_tm;
-
-	time(&rawtime);
-    local_tm = localtime(&rawtime);
-    utc_tm = gmtime(&rawtime);
-
-    // Calculate the time difference in minutes
-    int diff_minutes = (local_tm->tm_hour - utc_tm->tm_hour) * 60 + (local_tm->tm_min - utc_tm->tm_min);
-
-    // Adjust the difference if crossing a day boundary
-    if (local_tm->tm_yday < utc_tm->tm_yday) {
-        diff_minutes -= 24 * 60;
-    } else if (local_tm->tm_yday > utc_tm->tm_yday) {
-        diff_minutes += 24 * 60;
-    }
-
-    return diff_minutes;
+	TIME_ZONE_INFORMATION tz;
+	DWORD rc = GetTimeZoneInformation(&tz);
+	int offset_minutes = tz.Bias + (TIME_ZONE_ID_DAYLIGHT != rc ?  tz.StandardBias : tz.DaylightBias);
+	
+    return offset_minutes;
 }
 }
 
 
-#ifdef __WIN32__
 void bmx_current_datetime(SDateTime * dt, int utc) {
 void bmx_current_datetime(SDateTime * dt, int utc) {
 	SYSTEMTIME systemTime;
 	SYSTEMTIME systemTime;
     if (utc) {
     if (utc) {
@@ -967,6 +954,27 @@ void bmx_current_datetime(SDateTime * dt, int utc) {
     dt->offset = utc ? 0 : bmx_calc_timeoffset_mins();
     dt->offset = utc ? 0 : bmx_calc_timeoffset_mins();
 }
 }
 #else
 #else
+int bmx_calc_timeoffset_mins() {
+	time_t rawtime;
+    struct tm *local_tm, *utc_tm;
+
+	time(&rawtime);
+    local_tm = localtime(&rawtime);
+    utc_tm = gmtime(&rawtime);
+
+    // Calculate the time difference in minutes
+    int diff_minutes = (local_tm->tm_hour - utc_tm->tm_hour) * 60 + (local_tm->tm_min - utc_tm->tm_min);
+
+    // Adjust the difference if crossing a day boundary
+    if (local_tm->tm_yday < utc_tm->tm_yday) {
+        diff_minutes -= 24 * 60;
+    } else if (local_tm->tm_yday > utc_tm->tm_yday) {
+        diff_minutes += 24 * 60;
+    }
+
+    return diff_minutes;
+}
+
 void bmx_current_datetime(SDateTime * dt, int utc) {
 void bmx_current_datetime(SDateTime * dt, int utc) {
     struct timespec ts;
     struct timespec ts;
     struct tm *tm;
     struct tm *tm;