如何使文件创建,访问和修改日期与Windows属性相同?

问题描述 投票:11回答:2

我试图获得与Windows属性中显示的相同的创建,访问和修改日期,如下所示:

但我发现时间一直是30分钟:

相信它可能与时区/夏令时有关,但一直无法找到解决方案。试过看:TimeZone偏见并调整和查看不同的方法,包括:How to get create/last modified dates of a file in Delphi?

当前代码:

var
MyFd TWin32FindData;
FName: string;
MyTime: TFileTime;
MySysTime: TSystemTime;
myDate, CreateTime, AccessTime, ModTime: TDateTime; 
Begin
 ...
 FindFirstFile(PChar(FName), MyFd);
 MyTime:=MyFd.ftCreationTime;
 FileTimeToSystemTime(MyTime, MySysTime);
 myDate := EncodeDateTime(MySysTime.wYear, MySysTime.wMonth, MySysTime.wDay, MySysTime.wHour,
 MySysTime.wMinute, MySysTime.wSecond, MySysTime.wMilliseconds);
 Memo1.Lines.Add('Created: '+ FormatDateTime('dddd, d mmmm yyyy, hh:mm:ss ampm', MyDate));
 ...

任何帮助赞赏

谢谢保罗

windows delphi winapi
2个回答
27
投票

我不确定您当前的代码有什么问题,但我相信这些代码将使用标准的Windows API调用来满足您的需求。

procedure TMyForm.ReportFileTimes(const FileName: string);

  procedure ReportTime(const Name: string; const FileTime: TFileTime);
  var
    SystemTime, LocalTime: TSystemTime;
  begin
    if not FileTimeToSystemTime(FileTime, SystemTime) then
      RaiseLastOSError;
    if not SystemTimeToTzSpecificLocalTime(nil, SystemTime, LocalTime) then
      RaiseLastOSError;
    Memo1.Lines.Add(Name + ': ' + DateTimeToStr(SystemTimeToDateTime(LocalTime)));
  end;

var
  fad: TWin32FileAttributeData;

begin
  if not GetFileAttributesEx(PChar(FileName), GetFileExInfoStandard, @fad) then
    RaiseLastOSError;
  Memo1.Clear;
  Memo1.Lines.Add(FileName);
  ReportTime('Created', fad.ftCreationTime);
  ReportTime('Modified', fad.ftLastWriteTime);
  ReportTime('Accessed', fad.ftLastAccessTime);
end;

procedure TMyForm.Button1Click(Sender: TObject);
begin
  ReportFileTimes(Edit1.Text);
end;

3
投票

您应该能够使用以下代码将UTC日期时间值转换为本地日期时间值:

uses
  Windows;

function UTCTimeToLocalTime(const aValue: TDateTime): TDateTime;
var
  lBias: Integer;
  lTZI: TTimeZoneInformation;
begin
  lBias := 0;
  case GetTimeZoneInformation(lTZI) of
    TIME_ZONE_ID_UNKNOWN:
      lBias := lTZI.Bias;
    TIME_ZONE_ID_DAYLIGHT:
      lBias := lTZI.Bias + lTZI.DaylightBias;
    TIME_ZONE_ID_STANDARD:
      lBias := lTZI.Bias + lTZI.StandardBias;
  end;
  // UTC = local time + bias
  // bias is in number of minutes, TDateTime is in days
  Result := aValue - (lBias / (24 * 60));
end;

从你的图像来看,你的偏移实际上是10小时30分钟。你在南澳大利亚吗?

© www.soinside.com 2019 - 2024. All rights reserved.