The embarrassment of using TTimeSpan in Delphi 2010

I tried the new TTimeSpan record type in Delphi 2010. But I recommend a very strange problem.

assert(TTimeSpan.FromMilliseconds(5000).Milliseconds = 5000); 

This statement does not pass. The value "TTimeSpan.FromMilliseconds (5000) .Milliseconds" should be 5000, but it is 0.

I dig deeper:

 function TTimeSpan.GetMilliseconds: Integer; begin Result := Integer((FTicks div TicksPerMillisecond) mod 1000); end; FTicks = 50000000 TicksPerMillisecond = 10000 FTick div TicksPerMillisecond = 50000000 div 10000 = 5000 (FTick div TicksPerMillisecond) mod 1000 = 5000 mod 1000 = 0 // I do not understand, why mod 1000 Integer((FTick div TicksPerMillisecond) mod 1000) = Integer(0) = 0 

My interpretation of the code is correct, right?

UPDATE: the GetTotalMilliseconds (double precision) method is executed correctly.

+6
delphi record delphi-2010 timespan
source share
2 answers

You are misleading properties giving the total amount expressed in a given unit , while properties giving a part of the value when you break it down into its components (days, hours, minutes, seconds, milliseconds, ticks).

In doing so, you get an integer remainder for each category. Thus, Milliseconds will always be between 0 and 999 (the number of milliseconds per second is 1).
Or, another example, if you have 72 minutes, TotalMinutes is 72, but Minutes is 12 .

Very much like the DecodeDateTime function to break up a TDateTime .

And for what you want to achieve, you definitely need to use the TotalMilliseconds property, as TridenT pointed out, but the code for GetMilliseconds really correct in TimeSpan .

+7
source share

You should use TotalMilliseconds instead of the Milliseconds property.

It works better!

 assert(TTimeSpan.FromMilliseconds(5000).TotalMilliseconds = 5000); 

From the documentation:

TotalMilliseconds Double
The time interval expressed in milliseconds and part of milliseconds

+3
source share

All Articles