Re: YANFCQ [Yet Another Non-FastCode Question]: TDateTime -> uSec resolution




"Kristofer Skaug" wrote
> lately I've been struggling with some routines to squeeze
> microsecond information out of (and into) TDateTime values.
> A problem is that I cannot use DecodeTime() because it
> introduces rounding errors in determining the Millisecond
> count, which makes it impossible to reliably establish the
> "remainder" i.e. the small sub-millisecond fractional
> part "Microseconds in Second" [...]
> Any ideas [...] would be muchly appreciated!

Kristofer, This sounds familiar, so I searched
http://groups-beta.google.com/groups?as_q=FormatDateTime&as_oq=millisecond+milliseconds&as_eq=&as_ugroup=borland.public.*&as_uauthors=Herbster
and found the following:

The storage of today's date-time in TDateTime (i.e. double) vars
gives a quantization step of about 607 nanoseconds. The
decode date and time functions first do a *round to the nearest
millisecond-of-a-day*. Then they divide and remainder the
milliseconds out to the seconds, minutes, and hours.

Therefore, I suggest using trunc and frac to separate your
date-time values out into the milliseconds and submillisecond
parts before trying to use FormatDateTime or DecodeTime
or date, maybe like I have shown below. Rgds, JohnH

procedure TForm1.Button2Click(Sender: TObject);
var dt,dtms,dttm: TDateTime; dtfm: double; s, s1, s2: string;
begin
dt := NOW;
dtms := dt*(24*60*60*1000);
dttm := trunc(dtms)/(24*60*60*1000);
dtfm := frac(dtms);
s1 := FormatDateTime('YYYY-MM-DD HH:NN:SS.ZZZ',dttm);
s2 := FormatFloat('000',dtfm*1000);
s := s1+s2;
ShowMessage(s);
end;

.


Quantcast