getDateFileModified用于日光节省时间


function DateTimeToFileTime(FileTime: TDateTime): TFileTime;
var
  LocalFileTime, Ft: TFileTime;
  SystemTime: TSystemTime;
begin
  Result.dwLowDateTime := 0;
  Result.dwHighDateTime := 0;
  DateTimeToSystemTime(FileTime, SystemTime);
  SystemTimeToFileTime(SystemTime, LocalFileTime);
  LocalFileTimeToFileTime(LocalFileTime, Ft);
  Result := Ft;
end;
function ExtractShortDate(ATimeIn: TDateTime): string;
// Convert DateTime to short date string
begin
  Result := FormatDateTime('mm/dd/yyyy', ATimeIn);
end;
function ExtractTime(ATimeIn: TDateTime): string;
// Convert DateTime to am/pm time string
begin
  Result := FormatDateTime('hh:mm AM/PM', ATimeIn);
end;
function GetDateFileModified(AFileName: string): string;
// Return the file modified date as a string in local time
var
  SR: TSearchRec;
  UTCTime: Windows.TFileTime;
  GMTST: Windows.TSystemTime;
  LocalST: Windows.TSystemTime;
  ModifyDT: TDateTime;
  TZ: Windows._TIME_ZONE_INFORMATION;
begin
  Result := '';
  if FindFirst(AFileName, faAnyFile, SR) = 0 then
  begin
    UTCTime := SR.FindData.ftLastWriteTime;
    if FileTimeToSystemTime(UTCTime, GMTST) then
    begin
       // Get Timezone Information
      if GetTimeZoneInformation(TZ) <> 0 then
        if SystemTimeToTzSpecificLocalTime(@TZ, GMTST, LocalST) then
        begin
          ModifyDT := SystemTimeToDateTime(LocalST);
          Result := ExtractShortDate(ModifyDT) + ' ' + ExtractTime(ModifyDT);
        end
        else
        begin
          TaskMessageDlg('Unable To Convert Time', 'Unable to convert SystemTime To LocalTime',
            mtInformation, [mbOk], 0);
          Result := '';
          exit;
        end;
    end
    else
    begin
      TaskMessageDlg('Unable To Convert Time', 'Unable to convert FileTime To SystemTime',
        mtInformation, [mbOk], 0);
      Result := '';
      exit;
    end;
  end
  else
    TaskMessageDlg('File Not Found', ExtractFileName(AFileName) + ' does not exist.',
      mtInformation, [mbOk], 0);
  FindClose(SR);
end;

发布的原始代码未返回正确的时间。原始代码被工作代码替换,以便其他代码可能会发现这种有益的代码。

更新:该代码现在提供了正确的时间。

filetimetolocalfiletime的MSDN文档中突出显示了问题:

filetimetolocalfiletime使用当前设置用于时区和日光节省时间。因此,如果是日光保存时间,即使您要转换的时间是标准时间,此功能也会考虑日光保存时间。您可以将以下函数序列用作替代方案。

filetimetosystemime/systemtimetotzspecificallocaltime/systemTimetofiletime

您需要使用在更改之前创建的日光储蓄更改后查看文件时指定的三个函数(但是,当您和文件创建都在同一侧时,此方法当然也有效日光节省的变化)。

最新更新