[DUG] DateTime Floating Point Errors.

Kyley Harris kyleyharris at gmail.com
Sun Oct 23 19:25:39 NZDT 2005


I wonder if anyone else has noticed this irritating rounding issue on datetimes.

Bear in mind that this is not a useful answer is you care about
historical dates. I only care about dates greater than the year 2000.

Problem. When comparing an "empty date" (meaning Date = 0.0) delphi
behaves differently on different computers meaning that my if
varDateValue = 0.0 can return false a lot of the time. This is also
the same when comparing Dates that have a zero time factor.

on my computer, EncodeDate(1899,12,30) + EncodeTime(0,0,0,0) does not
return 0.0, but something along the lines of zero + an infintesimal
fraction that does not equal zero any more.

I solve my problem with the following method of forcing a date when
setting a date property.

  if DateTimeValue < ZeroDateThreshold /* EncodeDate(1900,1,1) */  then
  begin
    result := 0.0;
  end else
  begin
    result := RecodeMilliSecond(result,0);
  end;

This forces a removal of all these little tiny bits of rubbish on the
end of my datetimes that occurs when I convert my string dates
"yyyymmddhhnnsszzz" into real date etc.

anyone got better ideas? if not.. hope this helps anyone else.



--
Kyley Harris
Harris Software
+64-9-8455274
+64-21-671-821



More information about the Delphi mailing list