Delphi: How to determine and empty TDatetime value
First you need to define what you mean by 'an empty TDateTime
value'.
A TDateTime
value is a double with the date encoded in the integer part and the time encoded in the fractional part. So, the closest thing to a 'null date' you can get is probably 0
.
Hence, simply test ADate <> 0
to test if the date is 'null'.
But beware: if you declare a TDateTime
local variable then it will not necessarily be =0
before you give it a value. It can be anything. Of course, the same thing applies to variables of type integer
, double
, boolean
, ...
Also, I believe that a TDateTime
with value 0
encodes the date 1899-12-30.
Finally, negative TDateTime
values are perfectly normal. For instance, -5000
corresponds to 1886-04-22
.
I don't quite get the point of your code. If you want to use 0
as the 'unassigned' value (which is bad if you are interested in dates close to 1899-12-30), why not do simply
function IsUnassigned(ADate: TDateTime): boolean;
begin
result := ADate = 0;
end;
or, possibly (but not equivalently!),
function IsUnassigned(ADate: TDateTime): boolean;
begin
result := IsZero(Date);
end;
In his answer, ain gave a couple of more reasonable choices for the 'unassigned date' value.
At unit Spring.Persistence.Core.Session.pas
of library Spring Framework for Delphi (http://www.spring4d.org) in method TSession.ExecuteScalar<T>
in case of NULL for result used value Default(T);
I think, Your function can look like
function IsNull(ADate: TDateTime): Boolean;
begin
Result := ADate = Default(TDateTime);
end;
As Andreas already wrote, the TDateTime
type is actually double
and thus not "nullable". I use
const
c_UnassignedDate = -693594;
for a empty date value as this represents an impossible date of 00/00/0000
. But for example DevExpress uses
NullDate = -700000;
InvalidDate = NullDate + 1;
So there seems to be no agreed upon standard vale, you should pick one which suits your need.