[fpc-devel] NowUTC in FPC

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


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

> Is it possible to move NowUTC from LazUTF8SysUtils.NowUTC to FPC, e.g.
> DateUtils?
>
> LocalTimeToUniversal(Now) is inefficient and suffers from a race
> condition, but can still be used as a fallback implementation.
>

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

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);
end;
{$else}
begin
  // Beware, it suffers from a race condition!
  Result := LocalTimeToUniversal(Now);
end;
{$endif}


Denis
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160720/91392ba6/attachment.html>


More information about the fpc-devel mailing list