SystemTimeToFileTime Function

Declare Function SystemTimeToFileTime Lib "kernel32.dll" (lpSystemTime As SYSTEMTIME, lpFileTime As FILETIME) As Long

Platforms: Win 32s, Win 95/98, Win NT

SystemTimeToFileTime converts a time and date stored in a SYSTEMTIME structure to an identical time and date stored in a FILETIME structure. The former structure provides a easier way to access a date and time, whereas the latter is used by Windows to identify times and dates associated with files. The data put into the FILETIME structure identifies the same time and date as the source structure does. The function returns 0 if an error occured, or 1 if successful.

lpSystemTime
The date and time, in SYSTEMTIME format, to convert.
lpFileTime
Receives the date and time converted to FILETIME format.

Example:

' Determine if file C:\MyProgram\datafile.txt was created before
' Jan 5, 1999.  Note how CreateFile's alternate declare must be used under Win 95/98 --
' see that function's page for more information.
Dim hfile As Long  ' receives the handle to the file
Dim ctime As FILETIME  ' receives creation date and time of the file
Dim atime As FILETIME  ' receives last access date and time of the file
Dim wtime As FILETIME  ' receives last write-to date and time of the file
Dim jantime As SYSTEMTIME  ' will be set to Jan 5, 1999
Dim janfiletime As FILETIME  ' will receive analogous time as jantime
Dim comptimes As Long  ' receives comparison of ctime and janfiletime
Dim retval As Long  ' return value

' Get a handle to the file (note how the alternate declare is used):
hfile = CreateFileNS("C:\MyProgram\datafile.txt", GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, 0)
If hfile = -1 Then  ' if the file could not be opened
  Debug.Print "Could not open the file C:\MyProgram\datafile.txt."
  End  ' abort the program
End If

' Get the various times and dates associated with the file:
retval = GetFileTime(hfile, ctime, atime, wtime)
' Load jantime with the date January 5, 1999 at midnight:
jantime.wMonth = 1: jantime.wDay = 5: jantime.wYear = 1999
jantime.wHour = 0: jantime.wMinute = 0: jantime.wSecond = 0
' Convert jantime into FILETIME format so it can be compared with ctime:
retval = SystemTimeToFileTime(jantime, janfiletime)
' Compare the two times and display the relation:
comptimes = CompareFileTime(ctime, janfiletime)
If comptimes = -1 Then Debug.Print "File was created before midnight, January 5, 1999."
If comptimes = 0 Then Debug.Print "File was created at midnight, January 5, 1999."
If comptimes = 1 Then Debug.Print "File was created after midnight, January 5, 1999."

' Close the file
retval = CloseHandle(hfile)

See Also: FileTimeToSystemTime
Category: Time

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/s/systemtimetofiletime.html