Skip to content

Reject numeric components that fail to parse - #2

Merged
peterhellberg merged 1 commit into
peterhellberg:masterfrom
fbirds5230:fix/reject-invalid-number-components
Nov 26, 2026
Merged

peterhellberg merged 1 commit into
peterhellberg:masterfrom
fbirds5230:fix/reject-invalid-number-components

Conversation

@fbirds5230

Copy link
Copy Markdown
Contributor

Numeric components can match the duration pattern but fail strconv.ParseFloat. Those errors are currently ignored: Parse("PT1..5S") returns (0, nil), while Parse("P1Y..M") returns a year's duration with no error.

Return ErrUnsupportedFormat immediately when ParseFloat fails. The result is a zero duration, including when an earlier component was valid.

The new table tests cover malformed numbers in all seven units, invalid components before and after valid ones, and a negative duration. They also check the existing decimal and mixed week/time forms.

Validation:

  • Full go test ./... suite passes on both CI versions, Go 1.18.10 and 1.19.13.
  • The full suite also passes with race detection and coverage on Go 1.27.4; go vet ./... and go build ./... pass there as well.
  • Before the fix, the new cases report ten failures and five passing controls. After the fix, all fifteen pass.

Return ErrUnsupportedFormat when ParseFloat rejects a duration component,
instead of silently returning a zero or partial duration without an error.

Cover malformed numbers in every unit and preserve existing decimal and
mixed week/time inputs.
@peterhellberg

Copy link
Copy Markdown
Owner

@fbirds5230 Thank you for this improvement of the library.

@peterhellberg
peterhellberg merged commit 5ce38af into peterhellberg:master Nov 26, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants