HOWTO: FileTimeToLocalFileTime() Adjusts for Daylight Saving Time |
Q128126
Under NTFS, the API GetFileTime() returns the create time, last access
time, and last write time for the specified file. The times returned in the
FILETIME structures are in Universal Coordinated Time (UTC). This is also
the time that NTFS uses. You can use FileTimeToLocalFileTime() to convert a
file time to a local time. However, if you automatically adjust for
Daylight Saving Time, FileTimeToLocalFileTime() will adjust for Daylight
Saving Time based on whether the current date should be adjusted for
Daylight Saving Time, not based on whether the date represented by the
FILETIME structure should be adjusted.
The behavior in this situation is different under FAT, but may be changed
to match the behavior under NTFS in a future version of Windows NT.
The result of this behavior, which is by design, is that reported file times under NTFS may change with the start and end of Daylight Saving Time. For example, suppose that the file TEST.C has a last write FILETIME representing Jan 1, 1995 9:00pm (UTC), it is not Daylight Saving Time, and you are in the Pacific time zone. Both the DIR command and the following sample code report the file time as 1:00pm (LocalTime = UTC - 8).
#include <windows.h>
void main()
{
HANDLE hFile;
FILETIME ftCreate, ftLastAccess, ftLastWrite, ftLocal;
SYSTEMTIME st;
char buf[80];
// Open the file.
hFile = CreateFile( "test.c",
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
// Get the file time (in UTC) and convert to local time.
GetFileTime( hFile, &ftCreate, &ftLastAccess, &ftLastWrite );
FileTimeToLocalFileTime( &ftLastWrite, &ftLocal );
// Display the time, as a test.
FileTimeToSystemTime( &ftLocal, &st );
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, buf, sizeof(buf) );
MessageBox( NULL, buf, " FILE TIME", MB_OK );
}
Now, set the date to 7/1/95 and enable Automatically Adjust for Daylight
Saving Time. The DIR command and the sample code above will report the
file time as 2:00pm, because FileTimeToLocalFileTime() has adjusted for
Daylight Saving Time (LocalTime = UTC - 7).
#include <windows.h>
void main()
{
HANDLE hFile;
FILETIME ftCreate, ftLastAccess, ftLastWrite;
SYSTEMTIME stUTC, st;
char buf[80];
// Open the file.
hFile = CreateFile( "test.c",
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL );
// Get the file time (in UTC) and convert to local time.
GetFileTime( hFile, &ftCreate, &ftLastAccess, &ftLastWrite );
FileTimeToSystemTime( &ftLastWrite, &stUTC );
SystemTimeToTzSpecificLocalTime( NULL, &stUTC, &st);
// Display the time, as a test.
GetTimeFormat( LOCALE_USER_DEFAULT, 0, &st, NULL, buf, sizeof(buf) );
MessageBox( NULL, buf, "FILE TIME", MB_OK );
}
The FAT file system stores local time, not UTC. GetFileTime() gets cached
UTC times from FAT. In this sample, the time stored is 1pm and the cached
time is 9pm. When it becomes Daylight Saving Time, sample codes 1 and 2
will demonstrate the same behavior that they do under NTFS, because 9pm is
still used. However, when you restart the machine, the new cached time will
be 8pm (UTC = LocalTime + 7). The call to FileTimeToLocalFileTime() cancels
the adjustment made by GetFileTime() (LocalTime = UTC - 7). Therefore,
sample code 1 will report the correct time under FAT, but sample code 2
will not.
Additional query words:
Keywords : _IK kbAPI kbDateTime kbKernBase kbOSWinNT350 kbSDKWin32 kbDSupport kbGrpDSKernBase
Issue type : kbhowto
Technology : kbAudDeveloper kbWin32sSearch kbWin32API
|
Last Reviewed: October 22, 2000 © 2001 Microsoft Corporation. All rights reserved. Terms of Use. |