[fpc-devel] Add {$I %DATETIME%}

Denis Kozlov dezlov at gmail.com
Sat Feb 27 11:12:37 CET 2016


What if we define %TIMESTAMP% or %UNIXTIMESTAMP%, instead of %DATETIME%?

* %UNIXTIMESTAMP%  - as a well defined Unix timestamp (i.e. number of 
seconds since Unix Epoch).
* %TIMESTAMP%  - as an Int64 version of TTimeStamp (i.e. number of MSecs 
since 1/1/0001).

These could be inserted as plain old integers by the compiler, just like 
%LINENUM%, so there is no ambiguity nor implication of an agreement for 
encoding TDateTime as Double.

Sample code is attached. It demonstrates how to get both of these 
timestamps in complier with only a single dependency on SysUtils unit.

Any thoughts?

Denis


On 23/02/2016 11:57, Michael Van Canneyt wrote:
> I don't think this is a good idea.
>
> 1. The TDateTime format is not a basic format of the language.
>    It is an agreement on how to encode date/time information in a double.
>
>    You now make the compiler dependent on this "agreement", promoting
>    TDateTime to a compiler-recognized type.
>
> 2. Some targets do not even have floats. That will cause problems as 
> well.
>
> Michael.

-------------- next part --------------
program TimestampIncludeDirectives;

uses
  SysUtils;

function GetTimestampStr: String;
begin
  GetTimestampStr := IntToStr(Round(TimeStampToMSecs(DateTimeToTimeStamp(Now))));
end;

function GetUnixTimestampStr: String;
  function DateTimeDiff(const ANow, AThen: TDateTime): TDateTime;
  begin
    Result:= ANow - AThen;
    if (ANow>0) and (AThen<0) then
      Result:=Result-0.5
    else if (ANow<-1.0) and (AThen>-1.0) then
      Result:=Result+0.5;
  end;
  function DateTimeToUnix(const AValue: TDateTime): Int64;
  begin
    DateTimeToUnix := Round(DateTimeDiff(AValue,UnixEpoch)*SecsPerDay);
  end;
begin
  GetUnixTimestampStr := IntToStr(DateTimeToUnix(Now));
end;

begin
  WriteLn(GetTimestampStr);
  WriteLn(GetUnixTimestampStr);
  ReadLn;
end.



More information about the fpc-devel mailing list