2

I'm trying to write a function which converts a time between timezones.

For example:

convertedTime = TimezoneConvert("15/03/2020 12:00", "Central Standard Time", "Turkey Standard Time")

Now... the way it works so far is that it loops through each time zone key in the registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones) and reads the "Std" and "Dlt" string values. Once it finds a matching string to the specified time zone parameter, it populates a TIME_ZONE_INFORMATION object with the relevant data.

Private Type TIME_ZONE_INFORMATION
    Bias As Long
    StandardName(0 To 31) As Integer
    StandardDate As SYSTEMTIME
    StandardBias As Long
    DaylightName(0 To 31) As Integer
    DaylightDate As SYSTEMTIME
    DaylightBias As Long
End Type

Afterwards it uses the information in this object to do the conversion.

The function currently works for daylight savings, but the user has to know that it's in effect and call the function like so:

convertedTime = TimezoneConvert("15/03/2020 12:00", "Central Summer Time", "Turkey Standard Time")

I was just wondering if there is anything in the registry or on windows itself that identifies if a time zone key such as "Central Standard Time" currently has daylight saving in effect? That way, I can modify the code in a way so that if "Central Standard Time" was entered as a parameter, it could return the correct time with daylight savings applied.

If not, does anyone have any suggestions as to how this might be achieved?

I've had a look at the windows api method GetTimeZoneInformation but it seems to only return a timezone object for the local time on the machine.

I know you can do this with an outlook object using the .ConvertTime method, however this application may be installed on machines where outlook is not present.

Markus
  • 2,265
  • 5
  • 28
  • 54
kerberonix
  • 41
  • 6
  • Have you read [this answer](https://stackoverflow.com/a/25042628/634824)? Also, have you considered invoking the .NET functions that do this easier, such as [`TimeZoneInfo.ConvertTimeBySystemTimeZoneId`](https://learn.microsoft.com/dotnet/api/system.timezoneinfo.converttimebysystemtimezoneid#System_TimeZoneInfo_ConvertTimeBySystemTimeZoneId_System_DateTime_System_String_System_String_)? – Matt Johnson-Pint Mar 16 '20 at 18:28
  • Thanks for showing me that answer, I'll take a look. We are not allowed to copy dll files to the machines this is to be installed on (which is rather annoying as it would have made it so much easier!) it has to be a pure VBA and windows api solution (if this is even possible) – kerberonix Mar 16 '20 at 18:46
  • Also, you should prefer using the "dynamic" time zone info functions wherever possible. [`EnumDynamicTimeZoneInformation`](https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-enumdynamictimezoneinformation), [`TzSpecificLocalTimeToSystemTimeEx`](https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-tzspecificlocaltimetosystemtimeex), [`SystemTimeToTzSpecificLocalTimeEx`](https://learn.microsoft.com/en-us/windows/win32/api/timezoneapi/nf-timezoneapi-systemtimetotzspecificlocaltimeex), etc. – Matt Johnson-Pint Mar 16 '20 at 19:23
  • You are about to introduce ambiguity into your implementation. When going back from DST to normal time, there's an interval of time where timepoint names are reused. Which one of the possible timepoints are you going to pick in this situation? What if a user requires you pick the respective other? – IInspectable Mar 17 '20 at 03:39

1 Answers1

0

I was just wondering if there is anything in the registry

Yes. You could check the key under HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones There will be a subkey under it if the timezone support the DST. You could try to open the subkey of the specified timezone, and if cannot find the subkey, the SupportsDaylightSavingTime is true. enter image description here

Drake Wu
  • 6,927
  • 1
  • 7
  • 30