I have a utility that compares a source and destination file date/time. This works in most cases but fails when comparing date/time for files in different time zones. So I need a UTC datetime routine.
From looking around it seems the Windows API GetFileTime will support this. I also found this wrapper procedure, but it crashes on the GetFileTime call when compiled under Delphi 2010.
Any ideas? Or does anyone have a snippet of code that handles UTC timezones etc that I can pass 2 filenames to that will tell me if they are different?
function CompareFileTimes(File1, File2 : String) : LongInt;
var
F1, F2 : THandle;
F1_CreateTime,
F1_LastAccess,
F1_LastWrite,
F2_CreateTime,
F2_LastAccess,
F2_LastWrite : PFileTime;
begin
//Initialize all variables
F1 := 0;
F2 := 0;
//Since these are pointers, we have to
//allocate memory for the FileTime structures
GetMem(F1_CreateTime, SizeOf(TFileTime));
GetMem(F1_LastAccess, SizeOf(TFileTime));
GetMem(F1_LastWrite, SizeOf(TFileTime));
GetMem(F2_CreateTime, SizeOf(TFileTime));
GetMem(F2_LastAccess, SizeOf(TFileTime));
GetMem(F2_LastWrite, SizeOf(TFileTime));
//Fill the structures with nulls for now
FillChar(F1_CreateTime, SizeOf(TFileTime), #0);
FillChar(F1_LastAccess, SizeOf(TFileTime), #0);
FillChar(F1_LastWrite, SizeOf(TFileTime), #0);
FillChar(F2_CreateTime, SizeOf(TFileTime), #0);
FillChar(F2_LastAccess, SizeOf(TFileTime), #0);
FillChar(F2_LastWrite, SizeOf(TFileTime), #0);
//Get file handles for the files in question
//Notice that even though we're using CreateFile
//the open disposition for the file is OPEN_EXISTING
F1 := CreateFile(PChar(F1), 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
F2 := CreateFile(PChar(F2), 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
//Get the file times for the files.
GetFileTime(F1, F1_CreateTime, F1_LastAccess, F1_LastWrite);
GetFileTime(F2, F2_CreateTime, F2_LastAccess, F2_LastWrite);
//Assign the function's result to comparison
//-1, File1 is younger than File2
//0, File1 is the same as File2
//+1 File1 is older than File2
Result := CompareFileTime(F1_CreateTime^, F2_CreateTime^);
//Free the memory allocated to the pointers
FreeMem(F1_CreateTime, SizeOf(TFileTime));
FreeMem(F1_LastAccess, SizeOf(TFileTime));
FreeMem(F1_LastWrite, SizeOf(TFileTime));
FreeMem(F2_CreateTime, SizeOf(TFileTime));
FreeMem(F2_LastAccess, SizeOf(TFileTime));
FreeMem(F2_LastWrite, SizeOf(TFileTime));
end;
Any help will be greatly appreciated.