Mega Search
23.2 Million


Sign Up

Make a donation  
Conversion TDateTime to TDate  
News Group: embarcadero.public.delphi.language.delphi.general

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

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 22-Jan-2015, at 10:23 PM EST
From: Christian Kaufmann
 
Re: Conversion TDateTime to TDate  
News Group: embarcadero.public.delphi.language.delphi.general
> {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

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 23-Jan-2015, at 1:19 AM EST
From: Leif Uneus