Delphi Tutorial: Convert TFileTime to TDateTime.

 

The Object Pascal function FindFirst returns a TSearchRec which contains FindData. The structure of FindData is TWin32FindData corresponds to the WIN32_FIND_DATA structure as returned by the FindFirstFile API function.

 

The TWin32FindData record structure declared in Windows.pas. Borland has not provided any information about the structure so we need to use both Windows.pas and the Microsoft Win32 Programmers Reference to determine what each field means.

 

// uses Windows.pas

 

 

  _WIN32_FIND_DATAA = record

    dwFileAttributes: DWORD;                          // file attributes

    ftCreationTime: TFileTime;                        // Created date and time

    ftLastAccessTime: TFileTime;                      // Last accessed date and time    

    ftLastWriteTime: TFileTime;                       // Modified date and time

    nFileSizeHigh: DWORD;                             // file size high word

    nFileSizeLow: DWORD;                              // file size low word

    dwReserved0: DWORD;                               // reserved for future use

    dwReserved1: DWORD;                               //reserved

    cFileName: array[0..MAX_PATH - 1] of AnsiChar;    // full file name

    cAlternateFileName: array[0..13] of AnsiChar;     // alternate (short) file name

  end;

 

 

ftCreationTime, ftLastAccessTime and ftLastWriteTime contain the file date and time information. The data is in the TFileTime format which corresponds to Windows FILETIME format. It expresses the time in Coordinated Universal Time (UTC) format. The structure is:

 

typedef struct _FILETIME { // ft 

    DWORD dwLowDateTime;

    DWORD dwHighDateTime;

} FILETIME;

 

Three steps are necessary to convert this structure to an Object Pascal TDateTime variable.

1)      Convert the date from Coordinated Universal Time (UTC) to a local file time.

2)      Convert the local file time to system time.

3)      Convert the system time to a TDateTime variable.

 

Convert UTC to local time

 

The Win32 API function FileTimeToLocalFileTime converts a file time based on the Coordinated Universal Time (UTC) to a local file time. If the function succeeds it returns true, otherwise it returns false. It is declared in Windows.pas as:

 

// converts from UTC format to local file time

function FileTimeToLocalFileTime(const lpFileTime: TFileTime; var lpLocalFileTime: TFileTime): BOOL; stdcall;

 

 

We can use this function in our Delphi program as follows:

 

// FileTime : TFileTime                        // The file time in UTC (e.g. ftCreationTime)

// LocalTime : TFileTime;                      // A variable of TFileTime to hold the local time

 

FileTimeToLocalFileTime(FileTime, LocalTime)

 

 

Convert local file time to system time

 

The Win32 API function FileTimeToSystemTime converts a file time to a TSystemTime data structure.  If the function succeeds it returns true, otherwise it returns false. It is declared in Windows.pas as:

 

// converts from localfiletime to System Time (which is simply TDateTime)

function FileTimeToSystemTime(const lpFileTime: TFileTime; var lpSystemTime: TSystemTime): BOOL; stdcall;

 

 

We can use it in Delphi as follows:

 

// LocalTime : TFileTime;           - the file time as a TFileTime data structure

// SystemTime : TSystemTime;        - a TSystemTime variable to hold the result of the conversion.

 

FileTimeToSystemTime(LocalTime, SystemTime) then

 

Convert system time to TDateTime

 

The Object Pascal function SystemTimeToDateTime can be used to convert a value from Win32 TSystemTime format to a TDateTime value. The declaration can be found in online help and is part of the SysUtils unit.

 

function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;

 

We can use it in our Delphi program as:

 

 

LocalTime : TFileTime;

// SystemTime : TSystemTime;               // a TSystemTime variable

// DateTime : TDateTime;                   // a TDateTime variable to hold the result of the conversion

 

DateTime := SystemTimeToDateTime(SystemTime);

 

 

Combine it all together

 

Combining all three steps into a single function yields the following:

 

// convert TFileTime to a TDateTime

Function FileTimeToDateTime(FileTime : TFileTime) : TDateTime;

var

   LocalTime : TFileTime;

   SystemTime : TSystemTime;

 

begin

 

   Result := EncodeDate(1900,1,1);      // set default return in case of failure;

 

   if FileTimeToLocalFileTime(FileTime, LocalTime) then

      if FileTimeToSystemTime(LocalTime, SystemTime) then

         Result := SystemTimeToDateTime(SystemTime);

 

end;  // function FileTimeToDateTime

 

 

References

 

References on your computer.

 

WIN32.HLP

 

The Microsoft Win32 Programmers Reference

Windows.pas

 

Borland Delphi Run-time Library - Win32 API Interface Unit.  

 

Internet

 

Other tutorials

 

Other tutorials in this series

The Jedi Code Library

 

An excellent cooperative effort among Delphi programmers with lots of free source code

The MER System Database Search

 

A free newsgroup search engine you can use to find Delphi newsgroup postings

Microsoft Developers Network

 

The Microsoft support site for developers

Borland Developers Network

 

Borland’s support site.

Newsgroups

Borland Delphi newsgroups.

 

 

Books

 

The Tomes of Delphi: Win32 Core API - Windows 2000 Edition by John Ayers, ISBN 1-55622-750-7 Wordware Publishing Inc

 

The Tomes of Delphi: Win32 Shell API – Windows 2000 Edition by John Ayers, ISBN 1-55622-749-3 Wordware Publishing Inc

 

Windows 2000 SYSTEMS PROGRAMMING Black Book by Al Williams, 2000, ISBN 1-57610-280-7, The Coriolis Group

 

Misc.

Use of Source Code

The source code contained in this tutorial is provided free of charge, AS IS WITHOUT WARRANTY OF ANY KIND AND IS PROVIDED WITHOUT ANY IMPLIED WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY. You may incorporate this source code into your own program but you do so at your own risk. Please remember to test your program and any source code copied from this site. Please report errors and improvements to jpgriffiths.com.

Copyright

Copyright © 2002 Paul Griffiths,  www.jpgriffiths.com.

Last Update

Last Updated:  [December 2002]

 

 

 

Home | Tutorials Home