1)将ISO时间转换为Delphi TDateTime:
function ISOToDateTime(const AISODateTime: string): TDateTime;
var
I: Integer;
VDate, VTime: TDateTime;
VFormatSettings: TFormatSettings;
begin
// ISO format: 2009-07-06T01:53:23Z
VFormatSettings.DateSeparator := '-';
VFormatSettings.ShortDateFormat := 'yyyy-mm-dd';
VFormatSettings.TimeSeparator := ':';
VFormatSettings.ShortTimeFormat := 'hh:nn:ss';
I := Pos('T', AISODateTime);
VDate := StrToDate(Copy(AISODateTime, 1, I - 1), VFormatSettings);
VTime := StrToTime(Copy(AISODateTime, I + 1, 8), VFormatSettings);
Result := Trunc(VDate) + Frac(VTime);
end;
2)将UTC时间转换为本地时间:
function UniversalToLocalTime(const AUtcTime: TDateTime): TDateTime;
function _GetSystemTzOffset: Extended;
var
VTmpDate: TDateTime;
ST1, ST2: TSystemTime;
TZ: TTimeZoneInformation;
begin
GetTimeZoneInformation(TZ);
DateTimeToSystemTime(AUtcTime, ST1);
SystemTimeToTzSpecificLocalTime(@TZ, ST1, ST2);
VTmpDate := SystemTimeToDateTime(ST2);
Result := MinutesBetween(VTmpDate, AUtcTime) / 60;
if VTmpDate < AUtcTime then begin
Result := -Result;
end;
end;
var
VOffset: Extended;
begin
VOffset := _GetSystemTzOffset;
if VOffset = 0 then begin
Result := AUtcTime;
end else begin
Result := IncHour(AUtcTime, Trunc(VOffset));
Result := IncMinute(Result, Round(Frac(VOffset) * 60));
end;
end;