Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DateTimeToMilliseconds in DateUtils.pas is marked as internal?

Tags:

delphi

Why DateTimeToMilliseconds in DateUtils.pas is marked as internal? Can I use it?

{ Internal, converts a date-time to milliseconds }
function DateTimeToMilliseconds(const ADateTime: TDateTime): Int64;
var
  LTimeStamp: TTimeStamp;
begin
  LTimeStamp := DateTimeToTimeStamp(ADateTime);
  Result := LTimeStamp.Date;
  Result := (Result * MSecsPerDay) + LTimeStamp.Time;
end;

[Delphi XE]


I have found this on About.com:

Experience shows that creating two TDateTime values using the function and EncodeDateTime that are distant from each other only a millisecond, the function returns a MillisecondsBetween not return as was expected, proving that it is not accurate.

So, if I don't care about few milisecs, I should use it.

like image 801
Server Overflow Avatar asked Feb 17 '23 05:02

Server Overflow


1 Answers

The TDateTime is a floating point double. To minimize rounding errors when working with TDateTime values, most calculations in DateUtils converts the TDateTime to milliseconds.

Later when calculations are ready the Int64 value is converted back to a TDateTime value again.

The internal marking is to emphasize that this function is an implementation detail, not to be utilized outside the library. That is, when working with TDateTime values, use the public functions/procedures.

This is a little test of the function MilliSecondsBetween:

program TestMSecBetween;
{$APPTYPE CONSOLE}

uses 
  System.SysUtils,System.DateUtils;

var
  d1,d2 : TDateTime;
  i,iSec,iMin,iHour,iMSec;
  isb : Int64;
begin
  d1 := EncodeDateTime(2013,6,14,0,0,0,0);
  for i := 0 to 1000*60*60*24-1 do
  begin
    iHour := (i div (1000*60*60)) mod 24;
    iMin := (i div (1000*60)) mod 60;
    iSec := (i div 1000) mod 60;
    iMSec := i mod 1000;
    d2 := EncodeDateTime(2013,6,14,iHour,iMin,iSec,iMSec);
    isb := MilliSecondsBetween(d2,d1);
    if (isb <> i) then
      WriteLn(i:10,iHour:3,iMin:3,iSec:3,iMSec:4,isb:3);
  end;
  ReadLn;
end.

You can expand the test for more than one day to see if there are some anomalies.

like image 64
LU RD Avatar answered May 10 '23 23:05

LU RD