Hi everyone. I am sharing some cross-platform timing I have done, it would
be great to hear from you.
________________________________________
For mac I translated this python implementation to free pascal:
https://github.com/pupil-labs/pyuvc/blob/master/pyuvc-source/darwin_time.pxi
):
```
unit timestamps.machtime;

{$mode objfpc}{$H+}
{$modeswitch objectivec1}
{$linkframework CoreFoundation}

interface

type
  mach_timebase_info_data_t = record
    numer: UInt32;
    denom: UInt32;
  end;
  mach_timebase_info_t = ^mach_timebase_info_data_t;

function mach_absolute_time: UInt64; cdecl; external name
'mach_absolute_time';
function mach_timebase_info(info: mach_timebase_info_t): Integer; cdecl;
external name 'mach_timebase_info';

implementation

end.
```

However, I could not test it extensively. But here is some sample code:

```
uses ctypes,  timestamps.machtime;
{...}
var
  timeConvert: Float = 0.0;
//function get_sys_time_monotonic: Float;
function ClockMonotonic : Float;
var
  timeBase: mach_timebase_info_data_t;
begin
  if timeConvert = 0.0 then begin
    mach_timebase_info(@timeBase);
    timeConvert :=
      (Float(timeBase.numer) / Float(timeBase.denom) / Float(1000000000.0);
  end;
  Result := mach_absolute_time() * timeConvert;
end;
```
____________________________________________________
For linux, I am used to clock_gettime (
https://linux.die.net/man/3/clock_gettime):
```
unit timestamps.gnulinux;
uses Linux, UnixType, Math;
{...}
function ClockMonotonic: Float;
var
  tp: timespec;
  a, b : Float;
begin
  clock_gettime(CLOCK_MONOTONIC, @tp);
  a := Float(tp.tv_sec);
  b := Float(tp.tv_nsec) * 1e-9;
  Result := a+b;
end;
```
_________________________________________________
For windows I am used to QueryPerformanceCounter:
```
unit timestamps.windows;
uses Windows, Math;
{...}
var
  PerSecond : TLargeInteger;
function ClockMonotonic: Float;
var
  Count : TLargeInteger;
begin
  QueryPerformanceCounter(Count);
  Result := Float(Count) / Float(PerSecond);
end;

initialization
   QueryPerformanceFrequency(PerSecond);
```
Of course, you don't need any "ToSeconds" conversion if you don't want to.
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to