Article ID: 188768
Article Last Modified on 1/23/2007
typedef struct _FILETIME {
DWORD dwLowDateTime;
DWORD dwHighDateTime;
} FILETIME;
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:
#define _SECOND ((int64) 10000000) #define _MINUTE (60 * _SECOND) #define _HOUR (60 * _MINUTE) #define _DAY (24 * _HOUR)
ULONGLONG qwResult; // Copy the time into a quadword. qwResult = (((ULONGLONG) ft.dwHighDateTime) << 32) + ft.dwLowDateTime; // Add 30 days. qwResult += 30 * _DAY; // Copy the result back into the FILETIME structure. ft.dwLowDateTime = (DWORD) (qwResult & 0xFFFFFFFF ); ft.dwHighDateTime = (DWORD) (qwResult >> 32 );
BOOL SetFileTime(
HANDLE hFile, // Handle to the file.
CONST FILETIME *lpCreationTime, // Time the file was created.
CONST FILETIME *lpLastAccessTime, // Time the file was last accessed.
CONST FILETIME *lpLastWriteTime // Time the file was last
// written to.
);
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().
BOOL bResult;
HANDLE hFile = CreateFile( szFilename,
GENERIC_WRITE, // The file must be opened with write access.
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if (hFile != INVALID_HANDLE_VALUE) {
bResult = SetFileTime( hFile, &ft, NULL, NULL );
CloseHandle(hFile);
}
BOOL FileTimeToLocalFileTime(
CONST FILETIME *lpFileTime, // Pointer to UTC file time to convert.
LPFILETIME lpLocalFileTime // Pointer to converted file time.
);
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.
BOOL FileTimeToSystemTime(
CONST FILETIME *lpFileTime, // Pointer to file time to convert.
LPSYSTEMTIME lpSystemTime // Pointer to structure to receive
); // system time.
The SYSTEMTIME structure represents a date and time using individual
members for the month, day, year, weekday, hour, minute, second, and
millisecond.
int GetDateFormat(
LCID Locale, // Locale for which date is to be formatted.
DWORD dwFlags, // Flags specifying function options.
CONST SYSTEMTIME *lpDate, // Date to be formatted.
LPCTSTR lpFormat, // Date format string.
LPTSTR lpDateStr, // Buffer for storing formatted string.
int cchDate // Size of buffer.
);
int GetTimeFormat(
LCID Locale, // Locale for which time is to be formatted.
DWORD dwFlags, // Flags specifying function options.
CONST SYSTEMTIME *lpTime, // Time to be formatted.
LPCTSTR lpFormat, // Time format string.
LPTSTR lpTimeStr, // Buffer for storing formatted string.
int cchTime // Size of buffer.
);
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.
SYSTEMTIME st;
char szLocalDate[255], szLocalTime[255];
FileTimeToLocalFileTime( &ft, &ft );
FileTimeToSystemTime( &ft, &st );
GetDateFormat( LOCALE_USER_DEFAULT, DATE_LONGDATE, &st, NULL,
szLocalDate, 255 );
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, szLocalTime, 255 );
printf( "%s %s\n", szLocalDate, szLocalTime );
Keywords: kbinfo kbapi kbdatetime kbkernbase kbfileio KB188768