[Lazarus] How to program with time in milliseconds?

Michael Schnell mschnell at lumino.de
Fri May 23 10:41:15 CEST 2014


On 05/22/2014 10:32 AM, Michael Schnell wrote:
>
>
> Here I'd like to consider some suggestions (I of course can implement 
> this locally and we can later discuss a release for the fpc RTL or 
> whatever.
>
>  - function GetHardwareTicks could be a class function so I wold not 
> need to create an instance to use it
>  - FHWTickSupportAvailable could be calculated in the initialization 
> section so that it is not necessary to re-do this for any instance.
>  - property HWTickSupportAvailable could be a class property, as a 
> consequence.
>
>  - I'd like to use a "GetTicks" class-function that provides raw ticks 
> and automatically uses GetHardwareTicks if FHWTickSupportAvailable and 
> software ticks if not.

I could do this in a very straight forward way:

I moved several variables from TEpikTimer private to Implementation var:
var
   FSystemTicks:TimeBaseData; // The system timebase
   FHWCapabilityDataAvailable:Boolean; // True if hardware tick support 
is available
   FHWTickSupportAvailable:Boolean; // True if hardware tick support is 
available
   FHWTicks:TimeBaseData;     // The hardware timebase
   StartupCorrelationSample:TimebaseCorrelationData; // Starting ticks 
correlation snapshot
   FMicrosecondSystemClockAvailable:Boolean; // true if system has 
microsecond clock
   UpdatedCorrelationSample:TimebaseCorrelationData; // Snapshot of last 
correlation sample

(For this, some properties needed to be modified to use set procedures 
and get functions that access the variables.)


I created an initialization section:
Initialization
begin
   InitTimebases;
end;

(To do so I moved several functions out of the TEpikTimer class so there 
could referenced


I redefind a function to be a class function:
       class function GetHardwareTicks:TickType; // return raw tick 
value from hardware source
I defined a new class function:
       class function GetTicksFrequency:TickType;



I tested this and it works nice for me (Linux X86 32 bit):
After the start of the application, I can simply do 
TEpikTimer.GetTicksFrequency and TEpiktimer.GetHardwareTicks.

In fact This is all I need.

I feel that it does make sense not to call InitTimebases with any 
TEpiktimer.Create (Of course this also can be achieved by just checking 
if it already had been called. But even here the implementation 
independent variables are necessary.)

I don't know if the modified version of EpikTimer.pas in fact is viable 
as a base for future development.

-Michael







More information about the Lazarus mailing list