The date and time functions retrieve and set the date and time for the system and individual files.
There are five time formats. Time-related functions return time in one of these formats. You can also use the time functions to convert between time formats for ease of comparison and display. The following table summarizes the time formats.
- System Time (SYSTEMTIME) : Year, month, day, hour, second, and millisecond, taken from the internal hardware clock.
- Local Time (SYSTEMTIME or FILETIME) : A system time or file time converted to the system's local time zone.
- File Time (FILETIME) : 100-nanosecond intervals since January 1, 1601.
- MS-DOS Time (WORD) : A packed word for the date, another for the time.
- Windows Time (DWORD) : The number of milliseconds since the system booted; a quantity that cycles every 49.7 days.
System Time System time is the current date and time of day. The system keeps time so that your applications have ready access to accurate time. The system bases system time on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England.
Local Time While the system uses UTC-based time internally, your applications will generally display the local time — the date and time of day for your time zone. Therefore, to ensure correct results, you must be aware of whether a function expects to receive a UTC-based time or a local time, and whether the function returns a UTC-based time or a local time.
File Time A file time is a 64-bit value that represents the number of 100-nanosecond intervals that have elapsed since 12:00 A.M. January 1, 1601 (UTC). The system records file times whenever applications create, access, and write to files. FAT records file times in local time. NTFS records file times natively in FILETIME format, so they are not affected by changes in time zone or daylight saving time.
MS-DOS Date and Time MS-DOS date and MS-DOS time are packed 16-bit values that specify the month, day, year, and time of day an MS-DOS file was last written to. MS-DOS records the date and time whenever an MS-DOS application creates or writes to a file. MS-DOS applications retrieve this date and time using MS-DOS functions. When you use the GetFileTime function to retrieve the file times for files that were created by MS-DOS, GetFileTime automatically converts MS-DOS dates and times to UTC-based times.
Windows Time Windows time is the number of milliseconds elapsed since the system started running. This format exists primarily for backward compatibility with 16-bit Windows. To ensure that applications designed for 16-bit Windows continue to run successfully, the GetTickCountApi returns the current Windows time. |
Click here to copy the following block | Private Const GENERIC_READ = &H80000000 Private Const GENERIC_WRITE = &H40000000 Private Const FILE_SHARE_READ = &H1 Private Const FILE_SHARE_WRITE = &H2 Private Const CREATE_NEW = 1 Private Const CREATE_ALWAYS = 2
Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type
Private Type SYSTEMTIME wYear As Integer wMonth As Integer wDayOfWeek As Integer wDay As Integer wHour As Integer wMinute As Integer wSecond As Integer wMilliseconds As Integer End Type
Private Type TIME_ZONE_INFORMATION Bias As Long StandardName(31) As Integer StandardDate As SYSTEMTIME StandardBias As Long DaylightName(31) As Integer DaylightDate As SYSTEMTIME DaylightBias As Long End Type
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" ( _ ByVal lpFileName As String, _ ByVal dwDesiredAccess As Long, _ ByVal dwShareMode As Long, _ ByVal lpSecurityAttributes As Long, _ ByVal dwCreationDisposition As Long, _ ByVal dwFlagsAndAttributes As Long, _ ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" ( _ ByVal hObject As Long) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" ( _ ByVal lpFileName As String) As Long
Private Declare Function FileTimeToSystemTime Lib "kernel32" (lpFileTime As _ FILETIME, lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToFileTime Lib "kernel32.dll" ( _ ByRef lpSystemTime As SYSTEMTIME, _ ByRef lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToLocalFileTime Lib "kernel32" (lpFileTime As _ FILETIME, lpLocalFileTime As FILETIME) As Long
Private Declare Function LocalFileTimeToFileTime Lib "kernel32.dll" ( _ ByRef lpLocalFileTime As FILETIME, _ ByRef lpFileTime As FILETIME) As Long
Private Declare Sub GetSystemTimeAsFileTime Lib "kernel32.dll" ( _ ByRef lpSystemTimeAsFileTime As FILETIME)
Private Declare Function CompareFileTime Lib "kernel32.dll" ( _ ByRef lpFileTime1 As FILETIME, _ ByRef lpFileTime2 As FILETIME) As Long
Private Declare Function DosDateTimeToFileTime Lib "kernel32.dll" ( _ ByVal wFatDate As Long, _ ByVal wFatTime As Long, _ ByRef lpFileTime As FILETIME) As Long
Private Declare Function FileTimeToDosDateTime Lib "kernel32.dll" ( _ ByRef lpFileTime As FILETIME, _ ByVal lpFatDate As Long, _ ByVal lpFatTime As Long) As Long
Private Declare Function GetFileTime Lib "kernel32.dll" ( _ ByVal hFile As Long, _ ByRef lpCreationTime As FILETIME, _ ByRef lpLastAccessTime As FILETIME, _ ByRef lpLastWriteTime As FILETIME) As Long
Private Declare Function SetFileTime Lib "kernel32.dll" ( _ ByVal hFile As Long, _ ByRef lpCreationTime As FILETIME, _ ByRef lpLastAccessTime As FILETIME, _ ByRef lpLastWriteTime As FILETIME) As Long
Private Declare Function SystemTimeToVariantTime Lib "oleaut32.dll" ( _ ByRef lpSystemTime As SYSTEMTIME, _ ByRef vtime As Date) As Long
Private Declare Function VariantTimeToSystemTime Lib "oleaut32.dll" ( _ ByVal vtime As Date, _ ByRef lpSystemTime As SYSTEMTIME) As Long
Private Declare Function SystemTimeToTzSpecificLocalTime Lib "kernel32.dll" ( _ ByVal lpTimeZoneInformation As Any, _ ByRef lpUniversalTime As SYSTEMTIME, _ ByRef lpLocalTime As SYSTEMTIME) As Long
Const TEST_PATH = "C:\APIDemo.txt"
Dim hFile As Long, ret As Long
Private Sub Command1_Click() If File1.ListIndex < 0 Then MsgBox "Please first select file from the list", vbCritical Exit Sub End If
Call GetFileTimeDemo
Call SetFileTimeDemo End Sub
Private Sub SetFileTimeDemo() Dim ctime As FILETIME, atime As FILETIME, wtime As FILETIME, ctime_new As FILETIME Dim systime_utc As SYSTEMTIME
If hFile Then
GetSystemTimeAsFileTime ctime
VariantTimeToSystemTime DateAdd("d", -2, Now), systime_utc SystemTimeToFileTime systime_utc, ctime
rval = SetFileTime(hFile, ctime, atime, wtime)
GetSystemTimeAsFileTime ctime_new
ret = CompareFileTime(ctime, ctime_new) Select Case ret Case 1 MsgBox "File ctime (creation date/time has been modified). New ctime is greater than old ctime" Case -1 MsgBox "File ctime (creation date/time has been modified). New ctime is less than old ctime" Case 0 MsgBox "File ctime (creation date/time has been modified). New ctime is equal to old ctime" End Select
End If End Sub
Function GetFileTimeDemo() As Boolean Dim ftCreate As FILETIME Dim ftModify As FILETIME Dim ftLastAccess As FILETIME Dim ft As FILETIME Dim systime_utc As SYSTEMTIME, systime_local As SYSTEMTIME, snull As SYSTEMTIME Dim tz As TIME_ZONE_INFORMATION
Dim CreateDate As Date, LastAccessDate As Date, ModifyDate As Date
If hFile = 0 Then Exit Function
If GetFileTime(hFile, ftCreate, ftLastAccess, ftModify) Then
FileTimeToLocalFileTime ftCreate, ft LocalFileTimeToFileTime ft, ftCreate FileTimeToSystemTime ft, systime_utc CreateDate = DateSerial(systime_utc.wYear, systime_utc.wMonth, _ systime_utc.wDay) + TimeSerial(systime_utc.wHour, systime_utc.wMinute, _ systime_utc.wSecond) + (systime_utc.wMilliseconds / 86400000)
MsgBox "CompareFileTime Result Before LocalFileTimeToFileTime = " & CompareFileTime(ft, ftCreate) Dim ftUTC As FILETIME LocalFileTimeToFileTime ft, ftUTC MsgBox "CompareFileTime Result After LocalFileTimeToFileTime = " & CompareFileTime(ftCreate, ftUTC)
FileTimeToSystemTime ftLastAccess, systime_utc SystemTimeToTzSpecificLocalTime ByVal 0&, systime_utc, systime_local SystemTimeToVariantTime systime_local, LastAccessDate
FileTimeToSystemTime ftModify, systime_utc SystemTimeToTzSpecificLocalTime ByVal 0&, systime_utc, systime_local SystemTimeToVariantTime systime_local, ModifyDate End If
Dim msg
msg = "File Create Date/time : " & CreateDate & vbCrLf & _ "File LastAccess Date/time : " & LastAccessDate & vbCrLf & _ "File Last Modified Date/time : " & ModifyDate & vbCrLf
MsgBox msg, vbInformation, "File Time Demo" End Function
Private Sub Form_Load() hFile = CreateFile(TEST_PATH, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, CREATE_ALWAYS, 0, 0)
If hFile <= 0 Then MsgBox "Error#" & Err.LastDllError: Exit Sub Else MsgBox "New File Created >> " & TEST_PATH End If
Call GetFileTimeDemo Call SetFileTimeDemo Call GetFileTimeDemo
Call CloseHandle(hFile) ret = DeleteFile(TEST_PATH)
If ret <> 0 Then MsgBox "File deleted" End Sub |
|