-
-
Notifications
You must be signed in to change notification settings - Fork 234
Melhorias na exportação de um field para Date, Time ou DateTime. #425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
|
|
||
| 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; |
There was a problem hiding this comment.
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(...).
| 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(['"']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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; |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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(''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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); |
There was a problem hiding this comment.
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?
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.