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

27,043

Solution 1

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.

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;

Solution 2

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

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?

Share:
27,043
Paul Heinrich
Author by

Paul Heinrich

Updated on March 14, 2020

Comments

  • Paul Heinrich
    Paul Heinrich about 4 years

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

    File Properties

    But am finding the times are consistently 30 minutes out:

    File Properties 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?

    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

    • David Heffernan
      David Heffernan over 12 years
      You did not tell us how you are currently obtaining the information.
    • Paul Heinrich
      Paul Heinrich over 12 years
      David - addded the current code but had tried numerous methods
    • David Heffernan
      David Heffernan over 12 years
      OK, you are missing the conversion from UTC to local time. My answer and also Kobik's show how to do that.
  • Paul Heinrich
    Paul Heinrich over 12 years
    Henrick, Yes Adelaide South Australia. This was able to convert all the times to the same as in the windows properties. Appreciate your time
  • OnTheFly
    OnTheFly over 12 years
    You've got him! :-) Another way is FileTimeToLocalFileTime