Skip to content

Commit

Permalink
[winpr,timezone] implement GetDynamicTimeZoneInformation
Browse files Browse the repository at this point in the history
  • Loading branch information
akallabeth committed Apr 24, 2024
1 parent faaf0cd commit 615b6f0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
4 changes: 4 additions & 0 deletions winpr/include/winpr/timezone.h
Expand Up @@ -57,6 +57,10 @@ extern "C"
} DYNAMIC_TIME_ZONE_INFORMATION, *PDYNAMIC_TIME_ZONE_INFORMATION,
*LPDYNAMIC_TIME_ZONE_INFORMATION;

#define TIME_ZONE_ID_UNKNOWN 0
#define TIME_ZONE_ID_STANDARD 1
#define TIME_ZONE_ID_DAYLIGHT 2

WINPR_API DWORD GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation);
WINPR_API BOOL SetTimeZoneInformation(const TIME_ZONE_INFORMATION* lpTimeZoneInformation);
WINPR_API BOOL SystemTimeToFileTime(const SYSTEMTIME* lpSystemTime, LPFILETIME lpFileTime);
Expand Down
36 changes: 30 additions & 6 deletions winpr/libwinpr/timezone/timezone.c
Expand Up @@ -29,6 +29,10 @@

#define TAG WINPR_TAG("timezone")

#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif

#if defined(WITH_TIMEZONE_BUILTIN)
#include "WindowsZones.h"
#include "TimeZones.h"
Expand Down Expand Up @@ -700,9 +704,7 @@ DWORD GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
*tz = *dtz;

free(dtz);
/* 1 ... TIME_ZONE_ID_STANDARD
* 2 ... TIME_ZONE_ID_DAYLIGHT */
return local_time->tm_isdst ? 2 : 1;
return local_time->tm_isdst ? TIME_ZONE_ID_DAYLIGHT : TIME_ZONE_ID_STANDARD;
}

/* could not detect timezone, use computed bias from tm_gmtoff */
Expand All @@ -712,7 +714,7 @@ DWORD GetTimeZoneInformation(LPTIME_ZONE_INFORMATION lpTimeZoneInformation)
if (ConvertUtf8ToWChar("Client Local Time", tz->StandardName, ARRAYSIZE(tz->StandardName)) <= 0)
WLog_WARN(TAG, "Failed to set default timezone name");
memcpy(tz->DaylightName, tz->StandardName, sizeof(tz->DaylightName));
return 0; /* TIME_ZONE_ID_UNKNOWN */
return TIME_ZONE_ID_UNKNOWN;
}

BOOL SetTimeZoneInformation(const TIME_ZONE_INFORMATION* lpTimeZoneInformation)
Expand Down Expand Up @@ -765,8 +767,30 @@ BOOL TzSpecificLocalTimeToSystemTime(LPTIME_ZONE_INFORMATION lpTimeZoneInformati

DWORD GetDynamicTimeZoneInformation(PDYNAMIC_TIME_ZONE_INFORMATION pTimeZoneInformation)
{
WINPR_UNUSED(pTimeZoneInformation);
return 0;
TIME_ZONE_INFORMATION tz = { 0 };
const DWORD rc = GetTimeZoneInformation(&tz);

WINPR_ASSERT(pTimeZoneInformation);
pTimeZoneInformation->Bias = tz.Bias;
memcpy(pTimeZoneInformation->StandardName, tz.StandardName,
MIN(sizeof(tz.StandardName), sizeof(pTimeZoneInformation->StandardName)));
pTimeZoneInformation->StandardDate = tz.StandardDate;
pTimeZoneInformation->StandardBias = tz.StandardBias;

memcpy(pTimeZoneInformation->DaylightName, tz.DaylightName,
MIN(sizeof(tz.DaylightName), sizeof(pTimeZoneInformation->DaylightName)));
pTimeZoneInformation->DaylightDate = tz.DaylightDate;
pTimeZoneInformation->DaylightBias = tz.DaylightBias;

memcpy(pTimeZoneInformation->TimeZoneKeyName, tz.StandardName,
MIN(sizeof(tz.StandardName), sizeof(pTimeZoneInformation->TimeZoneKeyName)));

/* https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/ns-timezoneapi-dynamic_time_zone_information
*
* we want transitions on fixed dates.
*/
pTimeZoneInformation->DynamicDaylightTimeDisabled = TRUE;
return rc;
}

BOOL SetDynamicTimeZoneInformation(const DYNAMIC_TIME_ZONE_INFORMATION* lpTimeZoneInformation)
Expand Down

0 comments on commit 615b6f0

Please sign in to comment.