How to convert a historical timestamp to a different time zone with DST in Delphi?

自作多情 提交于 2019-12-01 18:09:20

Delphi TZDB may be of use. It's main feature is that has a class that handles times using the tz database, which, if it contains "historical enough" data, would let you use UTC as an intermediary. The tz database aims to have rules for all the time zones throughout the world and the various time shifts for things like leap years, daylight savings time, calendar changes, etc. as they relate to UTC since the Unix epoch (Midnight, Jan 1, 1970).

Once you have the package installed, usage would be along the lines of the following:

function ConvertFromGMTToBST(const AGMTTime: TDateTime): TDateTime;
var
   tzGMT, tzBST: TTimeZone;
   UTCTime: TDateTime;
begin
    tzGMT := TBundledTimeZone.GetTimeZone('GMT');
    tzBST := TBundledTimeZone.GetTimeZone('BST');
    UTCTime := tzGMT.ToUniversalTime(AGMTTime);
    Result := tzBST.ToLocalTime(UTCTime);
end;

The above relies on a few assumptions. First of all, that GMT and BST are valid aliases in the tz database. If not, then you'll need to find the closest cities. (e.g. America/New_York). The second one is that I'm pretty sure my code is Delphi XE+ specific. TZDB claims to work on Delphi 6 and newer (and FreePascal) though, so the adjustments to work should be minor.

Unfortunately regional dates and times are very complex, especially if you stretch back much before the 20th century.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!