Solved

Variant and TDateTime

Posted on 2009-05-13
8
2,096 Views
Last Modified: 2012-06-27
Hi, how i can check il a "Variant" Parameter or Variable is a TDateTime or a Double?
0
Comment
Question by:ISIGest
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 17

Expert Comment

by:Shanmuga Sundaram
ID: 24372779
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24373427
That link didn't work for me... anyway, you can check what type a variant holds, by using the VarType function... see the example below.

regards
Hypo
procedure TForm1.Button1Click(Sender: TObject);
var aVar : Variant;
    aDateTime : TDateTime;
    aDouble : Double;
begin
  aVar := aDateTime;
  Case VarType(aVar) of
    varDate: ShowMessage('Variant is a date');
    varDouble: ShowMessage('Variant is a double');
    else ShowMessage('Variant is of some other type');
  end;
 
  aVar := aDouble;
  Case VarType(aVar) of
    varDate: ShowMessage('Variant is a date');
    varDouble: ShowMessage('Variant is a double');
    else ShowMessage('Variant is of some other type');
  end;
end;

Open in new window

0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24373433
case VarType(Param) of
  varDate: ShowMessage('Date : ' + FormatDateTime('dd/mm/yyyy hh:nn:ss', VarToDateTime(Param)));
  varDouble: ShowMessage('Double: ' + FloatToStr(Param));
end;
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:ISIGest
ID: 24383057
The VarType "varDouble" could not be a valid TDateTime but a normal Double value.
0
 
LVL 37

Expert Comment

by:Geert Gruwez
ID: 24383199
how did you get the value ?
that will determine your approach !
0
 
LVL 12

Expert Comment

by:Hypo
ID: 24383388
ISIGest: Sorry, but I really don't understand your comment... It didn't work for you?  you got a runtime error? Can you post some of your code, with an explaination of what you are trying to do, and what results you'd expect if it was working?
0
 

Author Comment

by:ISIGest
ID: 24393259
Ok...sorry...
If I test the variant variable with a VarType() function, the result can be varDouble if the variant value is a TDateTime like "15/05/2009 10.34" (in Double value format = 39948,440278), but it can be also a normal Double value like 350,25 or other value like 8540,69.
Ok, now how i can check if this value is a "valid" Double for a valid TDateTime?

Thanks.
0
 
LVL 12

Accepted Solution

by:
Hypo earned 500 total points
ID: 24393620
Ok,
so your question is actually how to identify that a double contains a value that is a valid datetime?

The problem is this, a TDateTime is in the sense of CPU, the same thing as a double, and that's the reason why the delphi compiler allows you to type myDateTime := myDouble; and vice versa. Any valid Double can also be a valid TDateTime.

So, If the code that wrote the datetime to the Variant varaible, wrote it as a double variable, then the Variant type will contain the type info varDouble, and only if the code that wrote the datetime was using a TDateTime variable, only then will the Variant contain the type info varDate.

The only things I can come up with for you to solve this are,
1:
If you have access to the code that's writing to the Variant variable, then you should make sure that it always uses a variable of type TDateTime when setting the Variants type.

2:
If you don't have access to that code, then you could perform an extra test on all Variants that you'd expect to be a datetime, but that is of type varDouble, to see that they are in va valid range. The current dates lay somewhere around 39000, each day is a new integer, so if 39000.xxx is today, then 39001 is tomorrow etc etc... If you have a range of dates that you consider to be valid, like from "Jan 1st, 1900" to "Dec 31th, 2099", then you can rule out alot of the doubles that don't contain valid dates, just by checking if the value is within that intervall. (see example below). It's not perfect, but I don't think you can get a perfect sollution for that problem, since a datetime is in some sense the same thing as a double.

regards
Hypo
Function IsValidDateTime(AVariant : Variant) : Boolean;
begin
  Case VarType(AVariant) of
    varDate: Result := True;
    varDouble: Result := (AVariant > EncodeDate(1900, 1, 1)) and (AVariant < EncodeDate(2100, 1, 1));
    else Result := False;
  end;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var A : Variant;
    B : Double;
begin
  A := Now;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
 
  B := 1/2;
  A := B;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
 
  B := Now;
  A := B;
  if IsValidDateTime(A)
    then ShowMessage('A contains a valid datetime')
    else ShowMessage('A does not contain a valid datetime');
end;

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Internet Explorer View Settings Question 15 111
Print Graphic and Text to Epson TM-T88v 12 242
FMX and jaudiotracker playing memory stream 29 77
Twebbrowser add css to the header 3 24
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now