如何使用DST将历史时间戳转换为不同的时区? [英] How to convert a historical timestamp to a different time zone with DST in Delphi?

查看:238
本文介绍了如何使用DST将历史时间戳转换为不同的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

有没有可以使用的VCL API或Win32 API?

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

推荐答案

Delphi TZDB 可能有用它的主要特点是具有使用 tz数据库处理时间的类,如果它包含历史足够的数据,将让您以UTC为中介。 tz数据库旨在为世界各地的所有时区制定规则,以及与Unix纪元相关的UTC(闰年,夏令时,日历变更等)与UTC相关的各种时间变化(Midnight,Jan 1, 1970年)。

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;

上述依赖于几个假设。首先,这个 GMT BST 是tz数据库中的有效别名。如果没有,那么你需要找到最近的城市。 (例如 America / New_York )。第二个是我很确定我的代码是Delphi XE +具体的。 TZDB声称在Delphi 6和更新版本(和FreePascal)上工作,所以调整工作应该是很小的。

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.

不幸的是,区域日期和时间非常复杂,特别是你可以在20世纪之前伸展很多。

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

这篇关于如何使用DST将历史时间戳转换为不同的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆