如何在Delphi中使用DST将历史时间戳转换为不同的时区?

发布于 2024-12-09 05:37:10 字数 135 浏览 6 评论 0原文

我需要在 Delphi (Win32) 中将历史时间戳从 GMT 转换为 BST。我无法使用操作系统中的当前区域设置进行转换,因为它没有历史时间的正确夏令时 (DST) 偏移量。

是否有我可以使用的 VCL API 或 Win32 API?

I need to convert a historical timestamp from GMT to BST in Delphi (Win32). I can't use the current regional settings in the OS to do the conversion because it won't have the correct daylight saving (DST) offset for the historical time.

Is there a VCL API or Win32 API I can use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许一世地老天荒 2024-12-16 05:37:10

Delphi TZDB 可能有用。它的主要功能是有一个使用 tz 数据库 处理时间的类,如果它包含“足够历史”的数据,会让你使用 UTC 作为中介。 tz 数据库旨在为世界各地的所有时区以及闰年、夏令时、日历更改等的各种时移制定规则,因为它们自 Unix 纪元以来与 UTC 相关(1 月 1 日午夜, 1970)。

安装该软件包后,使用将遵循以下原则:

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;

上述内容依赖于一些假设。首先,GMTBST 是 tz 数据库中的有效别名。如果没有,那么您需要找到最近的城市。 (例如美国/纽约)。第二个是我很确定我的代码是特定于 Delphi XE+ 的。 TZDB 声称可以在 Delphi 6 和更新版本(以及 FreePascal)上工作,因此对工作的调整应该很小。

不幸的是,地区日期和时间非常复杂,尤其是如果追溯到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文