From 8333927737bf4e2daa5146cbc17b4a830c5da0c7 Mon Sep 17 00:00:00 2001
From: Max Kellermann <max.kellermann@gmail.com>
Date: Mon, 3 Jan 2022 15:43:15 +0100
Subject: [PATCH] time/Zone: add native Windows implementation

---
 src/time/Zone.cxx | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/time/Zone.cxx b/src/time/Zone.cxx
index 9165a10d0..534657436 100644
--- a/src/time/Zone.cxx
+++ b/src/time/Zone.cxx
@@ -32,19 +32,35 @@
 
 #include "Zone.hxx"
 
+#ifdef _WIN32
+#include <profileapi.h>
+#include <sysinfoapi.h>
+#include <timezoneapi.h>
+#else
 #include <time.h>
+#endif
 
 int
 GetTimeZoneOffset() noexcept
 {
-	time_t t = 1234567890;
 #ifdef _WIN32
-	struct tm *p = gmtime(&t);
+	TIME_ZONE_INFORMATION TimeZoneInformation;
+	DWORD tzi = GetTimeZoneInformation(&TimeZoneInformation);
+
+	int offset = -TimeZoneInformation.Bias * 60;
+	if (tzi == TIME_ZONE_ID_STANDARD)
+		offset -= TimeZoneInformation.StandardBias * 60;
+
+	if (tzi == TIME_ZONE_ID_DAYLIGHT)
+		offset -= TimeZoneInformation.DaylightBias * 60;
+
+	return offset;
 #else
+	time_t t = 1234567890;
 	struct tm tm;
 	tm.tm_isdst = 0;
 	struct tm *p = &tm;
 	gmtime_r(&t, p);
-#endif
 	return t - mktime(p);
+#endif
 }