[fpc-devel] NowUTC in FPC

Denis Kozlov dezlov at gmail.com
Wed Jul 20 16:59:20 CEST 2016


On 20 July 2016 at 15:52, Denis Kozlov <dezlov at gmail.com> wrote:

>
> So, below is a tidy cross-platform implementation of NowUTC:
>

Oops, forgot the milliseconds, here it is:

function NowUTC: TDateTime;
{$ifdef WINDOWS}
var
  SystemTime: TSystemTime;
begin
  Windows.GetSystemTime(SystemTime);
  Result := SystemTimeToDateTime(SystemTime);
end;
{$else}
{$ifdef UNIX}
var
  tp: timeval;
begin
  fpgettimeofday(@tp,nil);
  Result := UnixToDateTime(tp.tv_sec);
  Result := IncMilliSecond(Result, tp.tv_usec div 1000);
end;
{$else}
begin
  // WARNING: Suffers from a race condition!
  Result := LocalTimeToUniversal(Now);
end;
{$endif}
{$endif}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160720/9aa8f925/attachment.html>


More information about the fpc-devel mailing list