Article ID: 188768 - View products that this article applies to.
This article was previously published under Q188768
A file time represents the specific date and time at which a given file was created, last accessed, or last written to. A file time is stored in a FILETIME structure. This structure is used with various Win32 API calls.
The FILETIME structure represents the number of 100-nanosecond intervals since January 1, 1601. The structure consists of two 32-bit values that combine to form a single 64-bit value.
Note that the FILETIME structure is based on 100-nanosecond intervals. It is helpful to define the following symbols when working with file times. For example:
Performing Arithmetics with File TimesIt is often necessary to perform a simple arithmetic on file times. For example, you might need to know when a file is 30 days old. To perform an arithmetic on a file time, you need to convert the FILETIME to a quadword (a 64-bit integer), perform the arithmetic, and then convert the result back to a FILETIME.
Assuming ft is a FILETIME structure containing the creation time of a file, the following sample code adds 30 days to the time:
Setting File TimesYou can set the file times for a file by using the SetFileTime() function.
This function lets you modify creation, last access, and last write times without changing the content of the file. To use this function, you must have a handle to the open file. This file handle can be obtained from a call to CreateFile() or OpenFile(). The file must be opened with GENERIC_WRITE access. After the file times have been set, you should release the file handle through a call to CloseHandle().
Assuming szFilename is a valid filename and ft is a FILETIME structure, the following sample code sets the creation date for the file to the time contained in ft:
Displaying File TimesThe file time is based on coordinated universal time (UTC). UTC-based time is loosely defined as the current date and time of day in Greenwich, England. You will most likely want to display the file time with respect to the local time (that is, the date and time of day for your time zone). To do this, you can use FileTimeToLocalFileTime() as follows:
Note that this function uses the current settings for the time zone and daylight saving time. Therefore, if it is daylight saving time, this function will take daylight saving time into account, even if the time you are converting is in standard time.
To display the file time in a meaningful way, you first need to convert it to a system time using FileTimeToSystemTime() as follows:
The SYSTEMTIME structure represents a date and time using individual members for the month, day, year, weekday, hour, minute, second, and millisecond.
It is also preferable to display the date and time in a format consistent with the current locale selected for the system. You can do this by using GetDateFormat() and GetTimeFormat() as follows:
By passing LOCALE_USER_DEFAULT as the first parameter to these functions, you tell them to format the passed date/time according to the default format for the current locale. In this case, you can pass NULL for the lpFormat parameters.
Assuming ft is a FILETIME structure containing a UTC value, the following sample code prints the date stored in ft:
Article ID: 188768 - Last Review: January 23, 2007 - Revision: 3.3
Contact us for more help
Connect with Answer Desk for expert help.