Conversion TDateTime to TDate |
|
Hi,
internally TDate and TDateTime base on the type Double. This means in the following case my TDate
variable still contains a fraction part:
var
a : TDate;
b : TDateTime;
begin
b := Now;
a := b;
end;
I tried to define a default conversion with a helper / operator. But it seems, this is not possible:
TDateHelper = record helper for TDate
class operator Implicit(const AValue: TDateTime): TDate;
end;
class operator TBSDateHelper.Implicit(const AValue: TDateTime): TDate;
begin
Result := Trunc(AValue);
end;
Is there another solution to extend the current types with this functionality? I would like to avoid
the introduction of my own record types for TDate/TDateTime:
type
TBSDate = record
strict private
FValue : TDate;
public
.....
end;
cu Christian
|
Re: Conversion TDateTime to TDate |
|
> {quote:title=Christian Kaufmann wrote:}{quote}
> Hi,
>
> internally TDate and TDateTime base on the type Double. This means in the following case my TDate
> variable still contains a fraction part:
>
> var
> a : TDate;
> b : TDateTime;
> begin
> b := Now;
> a := b;
> end;
>
> I tried to define a default conversion with a helper / operator. But it seems, this is not possible:
>
> TDateHelper = record helper for TDate
> class operator Implicit(const AValue: TDateTime): TDate;
> end;
>
> class operator TBSDateHelper.Implicit(const AValue: TDateTime): TDate;
> begin
> Result := Trunc(AValue);
> end;
>
> Is there another solution to extend the current types with this functionality? I would like to avoid
> the introduction of my own record types for TDate/TDateTime:
>
> type
> TBSDate = record
> strict private
> FValue : TDate;
> public
> .....
> end;
>
Unfortunately record/class helpers does not allow operator overloading.
So using your own TBSDate record with a wrapped TDate is one way to overcome this restriction.
Another way is to use an explicit call to the RTL,
{code}
a := DateUtils.DateOf(b);
{code}
/Leif
|