time/Zone: add native Windows implementation

This commit is contained in:
Max Kellermann 2022-01-03 15:43:15 +01:00 committed by Max Kellermann
parent db03db0dca
commit 8333927737
1 changed files with 19 additions and 3 deletions

View File

@ -32,19 +32,35 @@
#include "Zone.hxx" #include "Zone.hxx"
#ifdef _WIN32
#include <profileapi.h>
#include <sysinfoapi.h>
#include <timezoneapi.h>
#else
#include <time.h> #include <time.h>
#endif
int int
GetTimeZoneOffset() noexcept GetTimeZoneOffset() noexcept
{ {
time_t t = 1234567890;
#ifdef _WIN32 #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 #else
time_t t = 1234567890;
struct tm tm; struct tm tm;
tm.tm_isdst = 0; tm.tm_isdst = 0;
struct tm *p = &tm; struct tm *p = &tm;
gmtime_r(&t, p); gmtime_r(&t, p);
#endif
return t - mktime(p); return t - mktime(p);
#endif
} }