[fpc-devel] Problem with Now() and time changed by ntpd

Pete Cervasio cervasio at airmail.net
Thu Nov 3 22:59:00 CET 2011


On Thursday, November 03, 2011 03:11:37 pm Hans-Peter Diettrich wrote:
> Pete Cervasio schrieb:
> >  > Splitting the TDateTime into year, month etc. is done by a
> >  > DecodeDate...
> >  > 
> >  > function, that *assumes* that TDateTime contains a local time. When
> >  > you
> >  > 
> >  > feed it an UTC time, the result is unusable.
> > 
> > What? How does it assume it's in local time? It assumes it has received
> > the value you want decoded. The value 40850.5 treated as a TDateTime is
> > 03 NOV 2011 @ 12:00.
> 
> How do you get the starting date and time of the epoch?

With the constant UnixDateDelta.  Help says that it "Specifies the difference 
between TDateTime and TIME_T values".

Here's how I use it.  My main software project does all its work in UTC so I 
use this utc_now rather than the built-in now.  Conversion to local time is 
done only for display or logging purposes.

function utc_now: TDateTime;
var
  TimeVal: TTimeVal;
  TimeZone: PTimeZone;
begin
  TimeZone := nil;
  GetTimeOfDay (TimeVal, TimeZone);
  Result := (TimeVal.tv_Sec / SecsPerDay) 
          + ((TimeVal.tv_usec / 1000.0) / MSecsPerDay)
          + UnixDateDelta;
end;

Best regards,
Pete C.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20111103/8d4b12d1/attachment.html>


More information about the fpc-devel mailing list