如何获取与 Windows 属性相同的文件创建、访问和修改日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9209394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:12:00  来源:igfitidea点击:

How to get File Created, Accessed and Modified dates the same as windows properties?

windowsdelphiwinapi

提问by Paul Heinrich

I am trying to get the same Created, Accessed and Modified dates as appears in the windows properties as in:

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

File Properties

文件属性

But am finding the times are consistently 30 minutes out:

但我发现时间总是 30 分钟:

File Properties Delphi

文件属性 Delphi

Believe it may have something to do with timezones/daylight savings but have been unable to find a solution. Have tried looking at: TimeZone Bias and adjusting and looking at different methods including: How to get create/last modified dates of a file in Delphi?

相信它可能与时区/夏令时有关,但一直无法找到解决方案。曾尝试查看:时区偏差并调整和查看不同的方法,包括: 如何在 Delphi 中获取文件的创建/上次修改日期?

Current code:

当前代码:

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));
 ...

Any help appreciated

任何帮助表示赞赏

Thanks Paul

谢谢保罗

回答by David Heffernan

I'm not sure what's wrong with your current code, but I believe this code will do what you need, using standard Windows API calls.

我不确定您当前的代码有什么问题,但我相信这段代码会使用标准的 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;

回答by Henrick Hellstr?m

You should be able to use the code below to transform a UTC date time value to a local date time vale:

您应该能够使用下面的代码将 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;

Judging from your images your offset is actually 10 hours and 30 minutes. Are you located in South Australia?

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