Skip to content

Conversation

@weslleycapelari
Copy link
Contributor

Adicionado uma função para verificar e converter múltiplos formatos de data e hora para TDateTime, e adicionado a exportação correta para TDate, TTime ou TDateTime, independente do formato enviado na requisição.


function MatchRoute(const AText: string; const AValues: array of string): Boolean;
function IsDateTime(const AText: string): Boolean; overload;
function IsDateTime(const AText: string; out AParsedValue: TDateTime): Boolean; overload;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Boa noite!

Como essa função faz o parse e possui parâmetro de saída, seria legal ter um nome mais significativo como TryParseToDateTime(...).

Suggested change
function IsDateTime(const AText: string; out AParsedValue: TDateTime): Boolean; overload;
function TryParseToDateTime(const AText: string; out AParsedValue: TDateTime): Boolean; overload;

begin
Result := False;
AParsedValue := 0;
LText := AText.Trim.TrimLeft(['"']).TrimRight(['"']);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LText := AText.Trim.TrimLeft(['"']).TrimRight(['"']);
LText := AText.Trim([' ', '"']);

LFormatSettings.ShortTimeFormat := LTimeFormat;
AParsedValue := {$IF DEFINED(FPC)}StrToDateTimeDef{$ELSE}StrToTimeDef{$ENDIF}(LText, 0, LFormatSettings);
if (AParsedValue > 0) then Exit(True);
end;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notei que a função possui 3 loops (e sub loop).
Porque não definir a máscara de data/hora na inicialização, com um escopo estático? Assim não precisaria aumentar a complexidade do parser validando cada opção de formato.

Isso fortaleceria um padrão para parser de tipos de data/hora, padronizando os contratos de todos os endpoints para uma mesma formatação de entrada/saída.

raise EConvertError.Create('');
{$IF DEFINED(FPC)}
DecodeDate(LDateTime, LYear, LMonth, LDay);
Result := EncodeDate(LYear, LMonth, LDay);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realmente é necessário fazer esse encode/decode aqui?

LFormat := GetFormatSettings;
Result := StrToDate(Copy(LStrParam, 1, Length(FDateFormat)), LFormat);
if not IsDateTime(LStrParam, LDateTime) then
raise EConvertError.Create('');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise EConvertError.Create('');
raise EConvertError.CreateFmt('Could not parse TDateTime for "%s"', [LStrParam]);

if not IsDateTime(LStrParam, LDateTime) then
raise EConvertError.Create('');
{$IF DEFINED(FPC)}
DecodeTime(LDateTime, LHour, LMinute, LSecond, LMilliSecond);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realmente é necessário fazer esse encode/decode aqui?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants