[DUG] Reading binary data as date/time from registry

John Bird johnkbird at paradise.net.nz
Fri May 14 17:54:38 NZST 2010


I suspect you want code a bit like examples below I found - its old, and 
refers to NT/XP but I think its likely to be a good candidate:

http://delphi.about.com/od/delphitips2007/qt/directory_dates.htm
# Delphi's FileDateToDateTime converts the system time stamp of a file to a 
TDateTime value.
# FileTimeToSystemTime API converts a 64-bit file time to system time format 
(TSystemTime)
# Delphi's EncodeDateTime returns a TDateTime represented by a specified 
year, month, day, hour, minute, second, and millisecond.

Haven't used the FileTimetoSystemTime API...looks like it converts a file 
time to system time format. System time is based on Coordinated Universal 
Time (UTC).

below - 86400 is number of seconds in a day.
Looks to me times are stored as seconds since 1980 (Windows).   However I 
recall there might be some variants used in NTFS, where the granularity of 
times stored (less than a second?) is finer than that of FAT32 (~2 seconds). 
However I am pretty sure the FileDateToDateTime routines do each correctly.

[Aside - the different granularities creates subtle problems when copying 
only modified files from NTFS to FAT32, eg a USB drive.   Sometimes the same 
file is copied again by some routines, even though its the same modified 
date,
see below  function xfFilesChanged  which includes examples of using both 
FileDate and DateTime for file modification dates]

http://www.efg2.com/Lab/Library/Delphi/DatesAndTimes/index.html

Method 3.  Alternative for Windows NT -- InstallDate

  VAR
    InstallDate:  TDateTime;
    KeyValue   :  DWORD;
    Registry   :  TRegistry;
.. . .
  KeyValue := 0;   // 1/1/1980

  Registry := TRegistry.Create;
  TRY
    Registry.RootKey := HKEY_LOCAL_MACHINE;
    IF   Registry.OpenKeyReadOnly(
         'SOFTWARE\Microsoft\Windows NT\CurrentVersion')
    THEN KeyValue := Registry.ReadInteger('InstallDate');
  FINALLY
    Registry.Free
  END;
  // Simiilar to UNIX conversion
  InstallDate := EncodeDate(1970,1,1) + KeyValue/86400;
  LabelDateTimeValue.Caption := FormatDateTime('mm/dd/yyyy hh:nn:ss',
                                  InstallDate)

 SysUtils
FileGetDate  FileGetDate returns a DOS date-time stamp for the specified 
file.

Handle := FileOpen(FileName, fmOpenRead OR fmShareDenyNone);
DOSStamp := FileGetDate(Handle);
FileClose(Handle);
DateTimeStamp := FileDateToDateTime(DOSStamp);



function xfFilesChanged(file1: string; file2: string): integer;
//check 2 files modification date
//returns 0=both files same, 1=file1 later, 2=file2 later
//if within 10secs counted as same
//note if one file does not exist, returns other as later
var
  fileage1, fileage2: integer;
  dtdate1, dtdate2, dt10secs: TDateTime;
begin
  result := 0;
  fileage1 := FileAge(file1);
  fileage2 := FileAge(file2);
  if (fileage1 < 0) and (fileage2 > 0) then
  begin
    result := 2;
    exit;
  end;
  if (fileage2 < 0) and (fileage1 > 0) then
  begin
    result := 1;
    exit;
  end;
  if (fileage1 < 0) or (fileage2 < 0) then exit; //maybe both don't exist
  dtdate1 := FileDatetoDateTime(fileage1);
  dtdate2 := FileDatetoDateTime(fileage2);
  if dtdate1 = dtdate2 then
  begin
    result := 0;
    exit;
  end;
  //now if not same, check if within 10 secs
  //in case of different filesystems
  dt10secs := encodetime(0, 0, 10, 0);
  if (dtdate2 > (dtdate1 - dt10secs))
    and (dtdate2 < (dtdate1 + dt10secs)) then
  begin
    result := 0;
    exit;
  end;
  //still different
  if dtdate1 > dtdate2 then result := 1
  else result := 2;
end;

John


> Okay, thats not a date. Next possibility is something like the win32
> filetime structure. MS uses that for sql date time. Big int representing
> 100 nanosec intervals since 1601. There is an API for conversion of
> filetime to datetime but cant remember it. (I can do it in SQL but
> that's way bad way to go.)
>
> -- 
> Phil Scadden, Senior Scientist GNS Science Ltd 764 Cumberland St,
> Private Bag 1930, Dunedin, New Zealand Ph +64 3 4799663, fax +64 3 477 
> 5232
>
> Notice: This email and any attachments are confidential. If received in 
> error please destroy and immediately notify us. Do not copy or disclose 
> the contents.
>
> _______________________________________________
> NZ Borland Developers Group - Delphi mailing list
> Post: delphi at delphi.org.nz
> Admin: http://delphi.org.nz/mailman/listinfo/delphi
> Unsubscribe: send an email to delphi-request at delphi.org.nz with Subject: 
> unsubscribe 




More information about the Delphi mailing list