Skip to content

Fix ParseISOTime to handle short HH:MM format without seconds #23

@bero

Description

@bero

Summary

ParseISOTime accepted the HH:MM format pattern but crashed when parsing because it always tried to read seconds from position 7.

Bug Description

The function pattern matching allowed ##:## format:

if not MatchPattern('##:##:##:###', str)
  and not MatchPattern('##:##:##.###', str)
  and not MatchPattern('##:##:##', str)
  and not MatchPattern('##:##', str) then  // <-- Accepts HH:MM
    raise EBold.CreateFmt(sInvalidTimeFormat, [str]);
But then unconditionally tried to parse seconds:
s := StrToInt(copy(str, 7, 2));  // <-- Crashes on '14:30' with '' is not a valid integer

Steps to Reproduce

var dt: TDateTime;
begin
  dt := ParseISOTime('14:30');  // Raises EConvertError: '' is not a valid integer value
end;

Fix

Check string length before parsing seconds:

if Length(str) >= 8 then
begin
  s := StrToInt(copy(str, 7, 2));
  if s > 59 then
    raise EBold.CreateFmt(sInvalidTimeFormatLargeSecond, [str]);
end
else
  s := 0;

UnitTest added to testproject

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions