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

Sven Barth pascaldragon at googlemail.com
Fri Nov 4 11:56:14 CET 2011


Am 04.11.2011 11:28, schrieb Hans-Peter Diettrich:
> Sven Barth schrieb:
>
>>> According to Delphi help a TDateTime of 0.0 represents 12/30/1899 12:00
>>> am, while Wikipedia states "start counting the seconds from the Unix
>>> epoch of 1970-01-01T00:00:00 UTC".
>>
>> You are aware that the definition of TDateTime and that of the Unix
>> timestamp are not supposed to be the same? The original TDateTime
>> definiton (Delphi 1 and maybe also 2) started from 01/01/0001 and was
>> later changed to todays 12/30/1899 to be COM compatible.
>
> The use of different types, for Windows local and system time, requires
> either a conversion when a UTC (system) time is assigned to a TDateTime
> variable, or overloaded functions are required for dealing with such
> different encodings. Currently the conversion from SystemTime (record)
> into TDateTime (Double) makes the TDateTime contain the *local* time,
> not the UTC time. That's why I wonder how you want to place an true UTC
> time value into a TDateTime - can you give some code?
>

Huh? I still don't get your problem...

Nevertheless here is an example:

=== source begin ===

program utctest;

{$mode objfpc}{$H+}
{$apptype console}

uses
   Windows, SysUtils;

var
   st: TSystemTime;
   dt: TDateTime;
begin
   GetLocalTime(st);
   dt := SystemTimeToDateTime(st);
   Writeln(FormatDateTime('c', dt));
   GetSystemTime(st);
   dt := SystemTimeToDateTime(st);
   Writeln(FormatDateTime('c', dt));
   Readln;
end.

=== source end ===

As long as your time zone isn't UTC you will see two different times. 
For me the result was the following:

=== output begin ===

04.11.2011 11:48:16
04.11.2011 10:48:16

=== output end ===

>> And still: It does not matter whether we are talking about local or
>> UTC time here.
>
> It does, as long as there is no guarantee that UTC and local time start
> at the exactly same date and time.

The TDateTime type is completely timezone agnostic. It's integer part is 
the count of days since 12/30/1899 and it's fracture part is the part of 
24 hours. So by definition there is no timezone. And TSystemTime is 
timezone agnonstic as well. It's just a struct that contains year, 
month, day, hour, minute, second and millisecond. It can even hold a UTC 
time or a local time (see the declaration of 
SystemTimeToTzSpecificLocalTime). So if you do a 
SystemTimeToTzSpecificLocalTime twice you'll get a result where the time 
is off by twice your timezone offset.

Regards,
Sven



More information about the fpc-devel mailing list