Delphi: how to express empty TDateTime?

Is it possible to express the empty (zero) value of TDateTime as a constant? I tried TDateTime(0), TDateTime(0.0)and other things, but the compiler (Delphi 7) was not impressed.

I am currently using an initialized global variable:

const
   TDateTime_0: TDateTime = 0.0;

Such work. However, I just inherited a nice big bunch of Delphi 7 code, and I didn't use Turbo Pascal in the age ... That means I seriously need to clear my Delphi fu, and that makes me want to know.

In contrast, the compiler is completely satisfied with something like Integer(0). Its purpose in a variant leads to a value of type 0 ftInteger, whereas the purpose of a simple literal 0 leads to a variant of a type ftSmallInt.

Clarification: the goal is to pass an “empty” value of a certain type to functions that accept variants (including arrays of variants controlled by the compiler, known as an “array of constants”, as well as setters for things like TParameter.Value).

Clarification for Ken White : the problem here is mainly in type inference and overload resolution; the aforementioned "functions that accept options" are only a special case. Literals 0and are 0.0implicitly converted to TDateTime, so they can be assigned to vessels of this type (variables, record fields), and they can be used to initialize such vessels (i.e. functional parameters) without further. However, everything changes when the compiler must execute type output:

procedure foo (value: Double); overload; 
procedure foo (value: TDateTime); overload;

- Double, , (.. ). , Double , , . (, Delphi , Delphi 7):

type
   TSomeId = type Integer;

procedure foo (value: Integer  ); overload;  begin  WriteLn('Integer   ', value);  end;
procedure foo (value: TSomeId  ); overload;  begin  WriteLn('TSomeId   ', value);  end;
procedure foo (value: Double   ); overload;  begin  WriteLn('Double    ', value);  end;
procedure foo (value: TDateTime); overload;  begin  WriteLn('TDateTime ', value);  end;

procedure test_TYPE_Double;
var
   d: Double;
   t: TDateTime;
begin
   foo(Integer(0));
   foo(TSomeId(0));
   d := 0;  foo(d);
   t := 0;  foo(t);
end;

/ : TDateTime ( Double) , Integer TSomeId , . , .

+4
2

, , :

TMyOwnDouble = type double;
...
var a,b: TMyOwnDouble;
...
a:=MyOwnDouble(0.0); //invalid typecast
b:=0.0;  //no problem

? TDateTime , TDateTime, 0.0, :

caption:=DateTimeToStr(0.0); //it shows 30.12.1899

2 , :

function FloatToDateTime(const Value: Extended): TDateTime; //sysUtils unit

, , .

function VarFromDateTime(DateTime: TDateTime): Variant;  //Variants unit

DateTime. , :

var V: Variant;
...
V:=VarFromDateTime(0.0);
//V:=FloatToDateTime(0.0);  //works as well
Caption:=V;  //shows 0:00:00

, , , TDateTime_0, .

+3

'gg' .

-5

Source: https://habr.com/ru/post/1617525/


All Articles