Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi: How to determine and empty TDatetime value

Seems there is no way to assign NULL (either an "unassigned value" to TDateTime variables.

The only way I've imagined is using something like this:

function isNull(aDate : TDateTime) : boolean;
const NullDate = 0.0;
var aNullDate : TDatetime;
    ms : Int64;
begin
  aNullDate := NullDate;
  ms := MilliSecondsBetween(aDate,aNullDate);
  result := (ms = Int64(0));
end;

Is there anybody out who knows better solution what not overlaps 0 date value?

Are negative TDateTime values dangerous? (As an able resource for previous purpose)

like image 430
xsubira Avatar asked Feb 20 '13 16:02

xsubira


2 Answers

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.

like image 117
ain Avatar answered Oct 13 '22 17:10

ain


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.

like image 20
Andreas Rejbrand Avatar answered Oct 13 '22 15:10

Andreas Rejbrand