I just spent some time testing ChatGPTs ability to "understand" and explain Delphi source code. The results were actually quite good.
I asked ChatGPT "whatβs wrong with the following Delphi code?"
function IsoStringToDateTime(const ISODateTime: string): TDateTime;
const
ISOShortLen = 19;
ISOFullLen = 23;
var
y, m, d, h, n, s, z: Word;
begin
// ISODateTime should be in one of these formats:
// YYYY-MM-DDTHH:NN:SS, YYYY-MM-DD HH:NN:SS
// YYYY-MM-DDTHH:NN:SS.ZZZ, YYYY-MM-DD HH:NN:SS.ZZZ
if (Length(ISODateTime) <> ISOShortLen) and (Length(ISODateTime) <> ISOFullLen) then
raise EConvertError.Create('Invalid ISO date time string: ' + ISODateTime);
y := SysUtils.StrToInt(Copy(ISODateTime, 1, 4));
m := SysUtils.StrToInt(Copy(ISODateTime, 6, 2));
d := SysUtils.StrToInt(Copy(ISODateTime, 9, 2));
h := SysUtils.StrToInt(Copy(ISODateTime, 12, 2));
n := SysUtils.StrToInt(Copy(ISODateTime, 15, 2));
s := SysUtils.StrToInt(Copy(ISODateTime, 18, 2));
z := StrToIntDef(Copy(ISODateTime, 21, 3), 0); // Optional
Result := EncodeDate(y, m, d) + EncodeTime(h, n, s, z);
end;
and also "What does the following Delphi function do?"
function FileSizeToHumanReadableString(_FileSize: Int64): string;
begin
if _FileSize > 5 * OneExbiByte then
Result := Format(_('%.2f EiB'), [_FileSize / OneExbiByte])
else if _FileSize > 5 * OnePebiByte then
Result := Format(_('%.2f PiB'), [_FileSize / OnePebiByte])
else if _FileSize > 5 * OneTebiByte then
Result := Format(_('%.2f TiB'), [_FileSize / OneTebiByte])
else if _FileSize > 5 * OneGibiByte then
Result := Format(_('%.2f GiB'), [_FileSize / OneGibiByte])
else if _FileSize > 5 * OneMebiByte then
Result := Format(_('%.2f MiB'), [_FileSize / OneMebiByte])
else if _FileSize > 5 * OneKibiByte then
Result := Format(_('%.2f KiB'), [_FileSize / OneKibiByte])
else
Result := Format(_('%d Bytes'), [_FileSize]);
end;
The answers will surprise you. And these were the shocking answers.
The answers were actually quite interesting.