[DUG] Reading binary data as date/time from registry
Jan Bakuwel
jan.bakuwel at omiha.com
Mon May 17 17:54:40 NZST 2010
Hi all,
It took a while but I found the solution. Here's the code, perhaps it's
useful to someone else.
Many thanks for the suggestions.
Jan
function RegistryBinaryToDateTime (const Registry: TRegistry; const
sKey, sValue: string): TDateTime;
const
SECS_IN_DAY = 86400;
BILLS_CONSTANT = 10000000;
type
TbinData = array[0..7] of byte;
function u64(binData: TbinData): uint64;
var i: integer; m: uint64;
begin
Result := 0; m := 1;
for i := Low(binData) to High(binData) do
begin
Result := m * binData[i] + Result;
m := m * 256;
end;
end;
var
uDays, uSeconds: uint64;
binData: TbinData;
begin
if not Assigned(Registry)
then raise Exception.Create('Please open the registry before calling
this function.');
with Registry do
begin
if OpenKey (sKey, False) then // Open the
registry key
begin
ReadBinaryData(sValue, binData, 8); // Read value
from the registry
Result := EncodeDateTime(1601, 1, 1, 0, 0, 0, 0); // Set the epoch
according to Bill
uSeconds := u64(binData) div BILLS_CONSTANT; // Apply Bill's
constant (no, don't ask)
uDays := uSeconds div SECS_IN_DAY; // Calculate days
uSeconds := uSeconds - (uDays * SECS_IN_DAY); // and seconds
Result := IncDay (Result, uDays); // as
(apparently??) we can't just do IncSecond
Result := IncSecond (Result, uSeconds); // so first add
the days, then the remainder of the seconds
CloseKey; // Close the
registry key
end;
end;
end;
More information about the Delphi
mailing list