Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 862
Paul Heinrich Avatar asked Feb 09 '12 10:02

Paul Heinrich


People also ask

Can you search for a file by properties such as date modified and type of file?

To search for a file based on properties such as the date it was last modified or what kind of file it is (such as "Picture"), first tap or click the Search Tools tab and use the options in the Refine group, and then enter your search terms in the search box.

How do I find the original file creation date online?

Right click - select Properties. Click the Details tab. Look for the Origin section.

How can I get the date modified of a file?

Windows file properties You can also see the modified date by viewing the file properties. Right-click the file and select Properties. In the Properties window, the Created date, Modified date, and Accessed date is displayed, similar to the example below.


2 Answers

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;
like image 106
David Heffernan Avatar answered Sep 21 '22 14:09

David Heffernan


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?

like image 38
Henrick Hellström Avatar answered Sep 19 '22 14:09

Henrick Hellström