Reputation: 13355
Seems easy but I didn't find a way to convert an UTC datetime to a specified timezone. I found how to convert an UTC date time to local date-time, but now I want to convert to a specific timezone (i.e. for example Moscow time)
For example in c# we can do:
// user-specified time zone
TimeZoneInfo southPole =
TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
// an UTC DateTime
DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);
// DateTime with offset
DateTimeOffset dateAndOffset =
new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));
Console.WriteLine(dateAndOffset);
But how to do in Delphi ?
Upvotes: 2
Views: 915
Reputation: 241515
A few things:
"Antarctica/South Pole Standard Time"
isn't a real time zone identifier. I assume you gave that in jest, but it makes it unclear as to whether you want to use Windows time zone identifiers (like "Eastern Standard Time"
), or IANA time zone identifiers (like "America/New_York"
).
Assuming you want to use Windows identifiers, you can indeed use the functions in the Win32 API. The comment in the question suggested the wrong API however. You should instead use SystemTimeToTzSpecificLocalTimeEx
.
DYNAMIC_TIME_ZONE_INFORMATION
structure, which have been available since Windows Vista. To get one of those from a named Windows time zone identifier, use the EnumDynamicTimeZoneInformation
function to loop through the system time zones until you find the one matching on the TimeZoneKeyName
field.If you instead wanted to use IANA time zone identifiers, use the Delphi tzdb library, as shown in this post.
Upvotes: 9