【文件属性】:
文件名称:获取时区,根据时区做 UTC时间到本地时间之间转换
文件大小:173KB
文件格式:7Z
更新时间:2017-08-20 05:29:56
时区 UTCtoLocalDT LocalDTtoUTC
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;
//是Time zone information的缩写,描述时区的一些重要信息
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;
public
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;
public
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);
tempTimeZone := TTimeZone.Create;
tempTimeZone.TimeZoneName := tempTimeZoneStrings.Strings[iCir];
tempTimeZone.Display := tempReg.ReadString('Display');
tempTimeZone.Std := tempReg.ReadString('Std');
tempTimeZone.Dlt := tempReg.ReadString('Dlt');
tempReg.ReadBinaryData('TZI', tempTimeZone.TZI^, SizeOf(TRegTZIInfo));
FTimeZoneList.AddObject(tempTimeZone.Display, tempTimeZone);
if FDefaultTimeZone = nil 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';
FTimeZoneList := TStringList.Create;
FTimeZoneList.Sorted := True;
FDefaultTimeZone := nil;
CollectTimeZone;
end;
procedure TTimeZones.DestoryTimeZones;
var
iCir: Integer;
tempTimeZone: TTimeZone;
begin
for iCir := 0 to FTimeZoneList.Count - 1 do
begin
tempTimeZone := TTimeZone(FTimeZoneList.Objects[iCir]);
tempTimeZone.Free;
end;
FTimeZoneList.Clear;
FTimeZoneList.Free;
end;
destructor TTimeZones.Destroy;
begin
DestoryTimeZones();
inherited;
end;
function TTimeZones.FindTimeZone(const ADisplay: string): TTimeZone;
var
iIndex: Integer;
begin
Result := nil;
FTimeZoneList.Sort;
if FTimeZoneList.Find(ADisplay, iIndex) then
begin
Result := TTimeZone(FTimeZoneList.Objects[iIndex]);
end;
end;
{ TTimeZone }
constructor TTimeZone.Create;
begin
FTZI := GetMemory(SizeOf(TRegTZIInfo));
FillMemory(FTZI, SizeOf(TRegTZIInfo), 0);
end;
destructor TTimeZone.Destroy;
begin
FreeMemory(FTZI);
inherited;
end;
function TTimeZone.GetSelfTimeZoneInformation: TTimeZoneInformation;
begin
GetTimeZoneInformation(Result);
Result.Bias := TZI^.Bias;
Result.StandardBias := TZI^.StandardBias;
Result.StandardDate := TZI^.StandardDate;
Result.DaylightBias := TZI^.DaylightBias;
Result.DaylightDate := TZI^.DaylightDate;
end;
function TTimeZone.LocalDateTimeToUTC(const ALocalDateTime: TDateTime;
var AUTC: TDateTime): Boolean;
var
tempLocalToLocal: TDateTime;
iMilliSecond: Int64;
begin
Result := UTCToLocalDateTime(ALocalDateTime, tempLocalToLocal);
if Result then
begin
if tempLocalToLocal > ALocalDateTime then
begin
iMilliSecond := MilliSecondsBetween(tempLocalToLocal, ALocalDateTime);
AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond * -1);
end else
begin
iMilliSecond := MilliSecondsBetween(ALocalDateTime, tempLocalToLocal);
AUTC := IncMilliSecond(ALocalDateTime, iMilliSecond);
end;
Result := True;
end;
end;
function TTimeZone.UTCToLocalDateTime(const AUTC: TDateTime;
var ALocalDateTime: TDateTime): Boolean;
var
TimeZone: TTimeZoneInformation;
stUTC, stLC: SYSTEMTIME;
begin
Result := False;
TimeZone := GetSelfTimeZoneInformation;
DateTimeToSystemTime(AUTC, stUTC);
if SystemTimeToTzSpecificLocalTime(@TimeZone, stUTC, stLC) then
begin
ALocalDateTime := SystemTimeToDateTime(stLC);
Result := True;
end;
end;
end.
【文件预览】:
时区
----Unit1.ddp(51B)
----Project1.res(876B)
----Unit1.dcu(6KB)
----Project1.dsk(7KB)
----Unit1.~ddp(51B)
----Unit1.dfm(1KB)
----uTimeZonesMgr.~pas(6KB)
----uTimeZonesMgr.dcu(6KB)
----Project1.~dpr(229B)
----Project1.cfg(434B)
----Project1.dpr(229B)
----Unit1.~dfm(1KB)
----Project1.exe(398KB)
----Project1.dof(42KB)
----uTimeZonesMgr.pas(6KB)
----Unit1.~pas(2KB)
----Unit1.pas(2KB)
网友评论
- 代码太乱了,用处不大