Recent

Author Topic: Bug in fuction ParseDate in EditBtn.pas  (Read 906 times)

kolevs

  • Newbie
  • Posts: 6
Bug in fuction ParseDate in EditBtn.pas
« on: August 13, 2020, 09:57:17 am »
Hi,

While looking at the code in EditBtn.pas, I noticed some strange statement(may be bug):

Function ParseDate(S : String; Order : TDateOrder; Def: TDateTime) : TDateTime;

Var
  P,N1,N2,N3 : Integer;
  B : Boolean;

begin
  Result:=Def;
  P:=Pos(DefaultFormatSettings.DateSeparator,S);
  If (P=0) then
    Exit;
  N1:=StrToIntDef(Copy(S,1,P-1),-1);
  If (N1=-1) then Exit;
  Delete(S,1,P);
  P:=Pos(DefaultFormatSettings.DateSeparator,S);
  If (P=0) then
    Exit;
  N2:=StrToIntDef(Copy(S,1,P-1),-1);
  If (N1=0) then Exit;
  Delete(S,1,P);
  N3:=StrToIntDef(S,-1);
  If (N3=-1) then
    exit;
  Case Order of
    doYMD : B:=TryEncodeDate(N1,N2,N3,Result);
    doMDY : B:=TryEncodeDate(N3,N1,N2,Result);
    doDMY : B:=TryEncodeDate(N3,N2,N1,Result);
    else B:=false;
  end;
  If not B then // Not sure if TryEncodeDate touches Result.
    Result:=Def;
end;


This:
  N2:=StrToIntDef(Copy(S,1,P-1),-1);
  If (N1=0) then Exit;

shouldn't it be:
  If(N2=-1) then Exit;


Regards

wp

  • Hero Member
  • *****
  • Posts: 11912
Re: Bug in fuction ParseDate in EditBtn.pas
« Reply #1 on: August 13, 2020, 10:23:08 am »
Yes, looks wrong. But I think it is not relevant because the edit mask of TDateEdit is designed such that only numeric characters can be entered, and thus the center part of the date string will always be a valid number, i.e. StrToIntdef will never return the default value.

Even without the edit mask (when the code snipped is copied to another file) the parsing result is still correct because the invalid center part makes the final TryStrToDate fail.

Nevertheless, for the sake of code clarity, I fixed it in r63728.

kolevs

  • Newbie
  • Posts: 6
Re: Bug in fuction ParseDate in EditBtn.pas
« Reply #2 on: August 14, 2020, 10:26:39 am »
Thanks, you are very fast  :)

Sorry for the inconvenience, but I found another not very logical part in same file.
function ParseDateNoPredefinedOrder
internal procedure Split

    for i :=  1 to Length(Fmt) do
      if not (Fmt in Digits) then Sep := Sep + [Fmt];

Isn't that supposed Fmt to be 'dd.mm.yy' or something like this, but not Digits.
May be SDate should be used?

Regards

 

TinyPortal © 2005-2018