开发者

UTC support for comparing 2 files date/time

开发者 https://www.devze.com 2023-01-11 21:55 出处:网络
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.

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, Fil开发者_开发技巧e1 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.


Delphi 2010 already contains an easy wrapper for getting the UTC time in ioutils.pas. Here is a piece of code that compares UTC creation times of files.

uses
  IOUtils, DateUtils;

function CompareFileCreationTimeUtc(const aFile1, aFile2: String): integer;
var
  lCreationTime1: TDateTime;
  lCreationTime2: TDateTime;
begin

  lCreationTime1 := TFile.GetCreationTimeUtc(aFile1);
  lCreationTime2 := TFile.GetCreationTimeUtc(aFile2);

  Result := CompareDateTime(lCreationTime1, lCreationTime2);

end;


In case you don't want to use IOUtils routine and like to do it yourself, your function should be corrected like (untested):

function CompareFileTimes(File1, File2 : String) : LongInt;
var
  F1, F2: THandle;
  F1_CreateTime, F2_CreateTime: TFileTime;
begin
  //Fill the structures with nulls for now
  ZeroMemory(@F1_CreateTime, SizeOf(TFileTime));
  ZeroMemory(@F2_CreateTime, SizeOf(TFileTime));

  //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(File1), 0, FILE_SHARE_READ , nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
  F2 := CreateFile(PChar(File2), 0, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

  if (F1=INVALID_HANDLE_VALUE) or (F1=INVALID_HANDLE_VALUE) then
    RaiseLastOSError;

  //Get the file times for the files.
  if not GetFileTime(F1, @F1_CreateTime, nil, nil) or
     not GetFileTime(F2, @F2_CreateTime, nil, nil) then
    RaiseLastOSError;
  //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);
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号