Allow month names in StrToDate and StrToDateTime
Summary
Motivated by freepascal.org/lazarus/lazarus#39779 (closed) I thought about the issue that StrToDate and StrToDateTime allow only numeric values in their date/time parts. The format expected is taken from the ShortDateFormat of the FormatSettings record where the month is assumed to be in the one- or two-digit representation.
But why can there be only numbers in the month part? In the FormatSettings.ShortDateFormat the month can be specified as 'mmm' or even 'mmmm' - and this inserts the short or long month names in the strings created by DateToStr. However, these strings cannot be converted back to dates by calling the inverse StrToDate function? I know that Delphi does the same, it accepts only numeric month strings. But I don't see a real reason for this asymmetry.
Patch
I am enclosing a patch which extends the StrToDate function such that also the month text representations are accepted (both short and long forms). It requires only a few changes, and don't think that it will cause a noticeable speed drop. Since StrToDateTime internally calls StrToDate, it will be extended automatically.
Delphi compatibility is not affected for "number-only" date strings. In the extended syntax with month names, however, it will throw an exception while it will be accepted by the the FPC version. However, we already have this mild kind of incompatibility because we allow the input string to contain only a date part which is not accepted for Delphi either.
Example Project
Here is a simple project which raises an exception with current FPC; after application of my patch the project will run correctly:
program Project1;
uses
SysUtils;
var
fs: TFormatSettings;
d1, d2: TDateTime;
s: String;
begin
fs := FormatSettings;
fs.ShortDateFormat := 'yyyy/mmm/dd'; // Note the 3 'm'!
fs.DateSeparator := '-';
d1 := EncodeDate(2020, 1, 31);
s := DateToStr(d1, fs);
d2 := StrToDateTime(s, fs);
WriteLn(s, ' ', d1:0:0, ' ', d2:0:0);
ReadLn;
end.