tags:

views:

129

answers:

3

I am currently using the following guidelines to get a file's "LastAccessTime" with Delphi http://www.latiumsoftware.com/en/delphi/00007.php

Using FindNext, have access to a TSearchRec object from which I can access ftLastWriteTime which is of type TFileTime

when converting this to a TDateTime object (using the above source) and then outputting DateTimeToString I get the date and time out but the hour seems to be the sum of the two digits in the files ftLastWriteTime hour value.

i.e instead of getting 2009/09/03 13:45 I get 2009/09/03 04:45 or instead of 2009/09/03 17:45 I get 2009/09/03 08:45

Any comments are most welcome, thanks in advance

+1  A: 

Typical, 20 minutes after making my first post I solve my own problem.

The author to the linked code avbove had two versions of the same code the second one is posted here http://www.latiumsoftware.com/en/delphi/00051.php

Problem solved! - I reckon the DOS library's interpretation of the Win32 timestamps was incorrect and that carried over into the code that followed. Maybe not? i will investigate futher if time allows.

gcahill
+2  A: 

The timestamps are in UTC, not local time.

Loren Pechtel
+4  A: 

"As usual" ;-) I'll point to the DSiWin32 which includes function DSiGetFileTimes which returns creation time, last access time and last modification time.

function DSiFileTimeToDateTime(fileTime: TFileTime; var dateTime: TDateTime): boolean;
var
  sysTime: TSystemTime;
begin
  Result := FileTimeToSystemTime(fileTime, sysTime);
  if Result then
    dateTime := SystemTimeToDateTime(sysTime);
end; { DSiFileTimeToDateTime }

function  DSiGetFileTimes(const fileName: string; var creationTime, lastAccessTime,
  lastModificationTime: TDateTime): boolean;
var
  fileHandle            : cardinal;
  fsCreationTime        : TFileTime;
  fsLastAccessTime      : TFileTime;
  fsLastModificationTime: TFileTime;
begin
  Result := false;
  fileHandle := CreateFile(PChar(fileName), GENERIC_READ, FILE_SHARE_READ, nil,
    OPEN_EXISTING, 0, 0);
  if fileHandle <> INVALID_HANDLE_VALUE then try
    Result :=
      GetFileTime(fileHandle, @fsCreationTime, @fsLastAccessTime,
         @fsLastModificationTime) and
      DSiFileTimeToDateTime(fsCreationTime, creationTime) and
      DSiFileTimeToDateTime(fsLastAccessTime, lastAccessTime) and
      DSiFileTimeToDateTime(fsLastModificationTime, lastModificationTime);
  finally
    CloseHandle(fileHandle);
  end;
end; { DSiGetFileTimes }
gabr