I'm caching files locally in my 3 tier app. To check whether to read the file from the local cache or from the server I compare file dates. I've found that there when converting file dates to TDateTime and viceversa there are inconsistencies and values that should match rarely do. Here's some code that demostrates the problem
procedure TestFileDateConversion;
const
Dir = 'c:\TestDir\';
Filename = 'test.txt';
var
FileDate, NewFileDate: TDateTime;
FilePath: String;
FileHandle: THandle;
begin
ForceDirectories(Dir);
FilePath := concat(Dir, Filename);
// Create the file if it doesn't already exist
FileCreate(FilePath);
FileDate := now;
// Set the file date
try
FileHandle := FileOpen(FileName, fmOpenWrite OR fmShareDenyNone);
if FileHandle > 0 Then
FileSetDate(FileHandle, DateTimeToFileDate(FileDate));
finally
FileClose(FileHandle);
end;
// Check that the expected file date and the actual file date match
if (FileAge(FilePath, NewFileDate)) and (FileDate <> NewFileDate) then
ShowMessage('File dates do not match'); // More often than not, they don't
end;
I'm sure this is caused by some rounding issue. Does anybody know a way to fix it?