IntStrToDate() too lax with date string format
Original Reporter info from Mantis: skyweb
-
Reporter name:
Original Reporter info from Mantis: skyweb
- Reporter name:
Description:
I found that TryStrToDate() and StrToDate() can convert many forms of text to date value, some results is not right, and some forms should not be valid date form. like "002017-00002-000023", "00231232-0001-031", etc.
Steps to reproduce:
{ try this program }
program t1;
uses sysutils;
var
myfs: TFormatSettings;
procedure test(s: string);
var
dt: TDate;
begin
dt := StrToDate(s, myfs);
writeln(Format('%-30s => %s', ['StrToDate(''' + s + ''')', DateToStr(dt, myfs)]));
end;
begin
myfs := DefaultFormatSettings;
myfs.DateSeparator := '-';
myfs.ShortDateFormat := 'yyyy-mm-dd';
test('002017-01-01');
test('2017-0001-001');
test('002017');
test('0020170623');
test('0017');
test('003');
test('3');
test('0010-3');
test('0011213123-002-0003');
test('0011213123-0002535-0003556');
end.
Additional information:
The problem located at dati.inc, function IntStrToDate(), line 448: "val(s1, values[n], c);", and line 375 "s1:string[4];"
Because s1 is a 4 char string, so "s1 := '00231232';" equals "s1 := '0023';".
And function val() do not care about length of s1, so "0002" and "2" is the same to val().
in my opinion, '3' and '03' should be a valid date text, but '003', '0003', '00034' is not.
I have made a alternative IntStrToDate() and it's testing code, see the attachment.
Mantis conversion info:
- Mantis ID: 32098
- OS: all
- OS Build: all
- Platform: all
- Fixed in version: 3.1.1
- Fixed in revision: 36687 (#e9a3a807)
- Target version: 3.2.0