Recent

Author Topic: Is this a Bug in TryStrToDate when useFormat = 'dddddd' or is it a 'feature'?  (Read 1347 times)

EganSolo

  • Full Member
  • ***
  • Posts: 211
Run the following command-line program:

Code: Pascal  [Select][+][-]
  1. Program StrToDateBug;
  2. uses SysUtils;
  3.  
  4. Const
  5.     FullDateFormat = 'dddddd';
  6. var
  7.     S    : String;
  8.     D    : TDate;
  9.     Res : Boolean;
  10. Begin
  11.   D := Now;
  12.   S := FormatDateTime(FullDateFormat,D);
  13.   Writeln('Date of today is ' , S);
  14.   Res := TryStrToDate(S,D,FullDateFormat);
  15.   If Res
  16.   then Writeln('Date is valid')
  17.   else Writeln('Date is invalid');
  18.   Readln;
  19. End.
  20.  

Even though S is assigned the valid date from FormatDateTime, TryStrToDate returns an invalid response. I tracked the bug to the function IntStrToDate: It doesn't consider the case where the format is six 'd's (dddddd).

If it's a bug, I'd be happy to rewrite this routine, unless it was not meant to handle this format...  :)

ccrause

  • Sr. Member
  • ****
  • Posts: 358
If it's a bug, I'd be happy to rewrite this routine, unless it was not meant to handle this format...  :)
According to the description of TryStrToDate:
Quote
The Date must consist of 1 to three digits
This means that 'd' and 'dd' should be acceptable in your example,  while 'ddd' up to 'dddddd' should not be acceptable. So not a bug in my opinion.

winni

  • Hero Member
  • *****
  • Posts: 2280
Hi!

I don't understand the question.

What do you want to achieve with 'dddddd'??

Something like  050505050505 for today?

Where is the use for that??

Perplexed

Winni


wp

  • Hero Member
  • *****
  • Posts: 8278
winni, tthe "six-d format" allows using the FormatSettings.LongDateFormat in FormatDateTime(): https://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses
  4.   SysUtils;
  5. begin
  6.   WriteLn(FormatDateTime('dddddd', now));   // ---> "Freitag, 5. März 2021" (German language settings)
  7.   ReadLn;
  8. end.
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Bart

  • Hero Member
  • *****
  • Posts: 4222
    • Bart en Mariska's Webstek
ScanDateTime might do the trick.
The StrToDate by design only handles numeric input AFAIK.

So, unless Delphi accepts such a string as input, we won't implement it in StrToDate function.

Bart

 

TinyPortal © 2005-2018