Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Working with file time APIs

Total Hit ( 9609)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


Click here to download the attached file  


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

'////////////////////////////////////////////////////////
'//File Time API
'////////////////////////////////////////////////////////
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

  '//Display file times
  Call GetFileTimeDemo

  '//Set file times
  Call SetFileTimeDemo
End Sub

'//This will change File's Last Modified Time
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
    '//Get the current System Time as File Time Format

    'GetSystemTimeAsFileTime ctime '//Creation time
    'GetSystemTimeAsFileTime atime '//Last Accessed time

    GetSystemTimeAsFileTime ctime  '//Last Modified time

    '//Tweak time little bit and assign back to wtime
    VariantTimeToSystemTime DateAdd("d", -2, Now), systime_utc
    SystemTimeToFileTime systime_utc, ctime

    '//Set Creation time alone (ctime, atime both Blank)
    rval = SetFileTime(hFile, ctime, atime, wtime)

    '//////////////////////////////////////////////////////
    '//Compre time demo
    '//////////////////////////////////////////////////////
    GetSystemTimeAsFileTime ctime_new  '//Last Modified time

    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

' Retrieve the Create date, Modify (write) date and Last Access date of
' the specified file. Returns True if successful, False otherwise.

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

  ' read date information
  If GetFileTime(hFile, ftCreate, ftLastAccess, ftModify) Then


    '//File CreationDate

    '////////////////////////////////////////////////////////////////////////////////
    ''//first, convert UTC file time to local file time then convert to system time
    FileTimeToLocalFileTime ftCreate, ft
    LocalFileTimeToFileTime ft, ftCreate
    FileTimeToSystemTime ft, systime_utc
    ''// finally, make up the Date value
    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)

    '//File time comparision demo
    MsgBox "CompareFileTime Result Before LocalFileTimeToFileTime = " & CompareFileTime(ft, ftCreate)
    '                  '//-1 (First file time is less than second file time)
    '                  '// 0 (Both are equal)
    '                  '//+1 (First file time is greater than second file time)
    ''//Now convert Local file time back to UTC File time and compare with original time (just for demo)
    Dim ftUTC As FILETIME
    LocalFileTimeToFileTime ft, ftUTC
    MsgBox "CompareFileTime Result After LocalFileTimeToFileTime = " & CompareFileTime(ftCreate, ftUTC)
    '////////////////////////////////////////////////////////////////////////////////

    '//Or

    'FileTimeToSystemTime ftCreate, systime_utc
    'SystemTimeToTzSpecificLocalTime ByVal 0&, systime_utc, systime_local
    'SystemTimeToVariantTime systime_local, CreateDate '//same as DateSerial in VB as shown above in commented part


    '//File LastAccessedDate
    FileTimeToSystemTime ftLastAccess, systime_utc
    SystemTimeToTzSpecificLocalTime ByVal 0&, systime_utc, systime_local
    SystemTimeToVariantTime systime_local, LastAccessDate

    '//File LastModifiedDate
    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()
  '//Create and open file for demo
  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   '//Here we Change File Creation date/time (Now check the file creation time in msgbox)
  Call GetFileTimeDemo

  Call CloseHandle(hFile)
  ret = DeleteFile(TEST_PATH)

  If ret <> 0 Then MsgBox "File deleted"
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.