[Lazarus] Better implementation of GetTickcount using clock_gettime

Luca Olivetti luca at wetron.es
Tue Apr 1 12:20:53 CEST 2014

I use GetTickCount a lot to keep track of timeouts.
On windows, it is guaranteed to be monotonic, so my usual

  if GetTickCount-StartTime>Timeout then..

works fine (yes, even when the time wraps around, provided StartTime is
defined as DWORD and Timeout is smaller than a DWORD, of course I could
use GetTickCount64 but usually the 32bits version is enough).

For unix the implementation uses fpgettimeofday but that's not
monotonic, so it could break the above code.

For linux a more accurate implementation could be

function GetTickCount64: QWord;
  tp: timespec;
  clock_gettime(CLOCK_MONOTONIC, @tp);
  Result := (Int64(tp.tv_sec) * 1000) + (tp.tv_nsec div 1000000);

And I say linux because CLOCK_MONOTONIC is only defined there in fpc,
though it should be available even in other systems:


Luca Olivetti
Wetron Automation Technology http://www.wetron.es
Tel. +34 935883004  Fax +34 935883007

More information about the Lazarus mailing list