本工具提供便捷的时间转换服务,帮助用户轻松将UTC时间转换为所在地区的本地时间,适用于跨国通讯和全球旅行。
unit uTimeZonesMgr;
interface
uses Windows, SysUtils, Classes, Registry, DateUtils;
type
//用于读取时区注册表TZI(长度为44)的属性值,存储时区信息
PRegTZIInfo = ^TRegTZIInfo;
TRegTZIInfo = record
Bias: Longint;
StandardBias: Longint;
DaylightBias: Longint;
StandardDate: TSystemTime;
DaylightDate: TSystemTime;
end;
//单个时区管理对象
TTimeZone = class
private
FTimeZoneName: string; //时区的显示名
FDisplay: string; //夏令时的名字
FDlt: string; //时区标准名字
FStd: string;
FTZI: PRegTZIInfo;
function GetSelfTimeZoneInformation: TTimeZoneInformation;
public
constructor Create;
destructor Destroy; override;
function UTCToLocalDateTime(const AUTC: TDateTime; var ALocalDateTime: TDateTime): Boolean;
function LocalDateTimeToUTC(const ALocalDateTime: TDateTime; var AUTC: TDateTime): Boolean;
//属性定义
property TimeZoneName: string read FTimeZoneName write FTimeZoneName;
property Display: string read FDisplay write FDisplay;
property Dlt: string read FDlt write FDlt;
property Std: string read FStd write FStd;
property TZI: PRegTZIInfo read FTZI write FTZI;
end;
//所有时区管理对象
TTimeZones = class
private
FTimeZoneKeyPath: string;
FTimeZoneList: TStringList;
FDefaultTimeZone: TTimeZone;
procedure CollectTimeZone;
procedure DestoryTimeZones;
procedure CheckISDefaultTimeZone(ATimeZone: TTimeZone);
public
constructor Create;
destructor Destroy; override;
function FindTimeZone(const ADisplay: string): TTimeZone;
//属性定义
property TimeZoneList: TStringList read FTimeZoneList;
property DefaultTimeZone: TTimeZone read FDefaultTimeZone;
end;
implementation
{ TTimeZones }
procedure TTimeZones.CheckISDefaultTimeZone(ATimeZone: TTimeZone);
var
DefaultTimeZone: TTimeZoneInformation;
begin
GetTimeZoneInformation(DefaultTimeZone);
if (ATimeZone.TZI.Bias = DefaultTimeZone.Bias) and (ATimeZone.Std = DefaultTimeZone.StandardName)
then FDefaultTimeZone := ATimeZone;
end;
procedure TTimeZones.CollectTimeZone;
var
reg, tempReg: TRegistry;
tempKeyPath: string;
tempTimeZoneStrings: TStrings;
iCir: Integer;
tempTimeZone: TTimeZone;
begin
reg := TRegistry.Create;
try
reg.RootKey := HKEY_LOCAL_MACHINE;
//打开注册表键,获取所有时区信息。
reg.OpenKey(FTimeZoneKeyPath, False);
//创建一个新的字符串列表来存储时区名称。
tempTimeZoneStrings := TStringList.Create;
try
//读取注册表下的子项名
reg.GetKeyNames(tempTimeZoneStrings);
for iCir := 0 to tempTimeZoneStrings.Count - 1 do begin
tempKeyPath := FTimeZoneKeyPath + \ + tempTimeZoneStrings.Strings[iCir];
tempReg := TRegistry.Create;
try
//打开注册表键,读取时区信息。
tempReg.RootKey := HKEY_LOCAL_MACHINE;
tempReg.OpenKey(tempKeyPath, False);
//创建一个新的TTimeZone对象
tempTimeZone := TTimeZone.Create;
with tempTimeZone do begin
TimeZoneName := tempTimeZoneStrings.Strings[iCir];
Display := tempReg.ReadString(Display);
Std := tempReg.ReadString(Std);
Dlt := tempReg.ReadString(Dlt);
//读取注册表中的二进制数据到TZI属性中。
tempReg.ReadBinaryData(TZI, TZI^, SizeOf(TRegTZIInfo));
end;
FTimeZoneList.AddObject(tempTimeZone.Display, tempTimeZone);
if not Assigned(FDefaultTimeZone) then
CheckISDefaultTimeZone(tempTimeZone);
finally
//关闭注册表键,释放资源。
tempReg.CloseKey;
tempReg.Free;
end;
end;
finally
//清理字符串列表内存
tempTimeZoneStrings.Free;
end;
finally
reg.CloseKey;
reg.Free;
end;
end;
constructor TTimeZones.Create;
begin
FTimeZoneKeyPath := \SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones;