Skip to content

Parse durations exactly and fix edge cases - #4

Merged
peterhellberg merged 4 commits into
masterfrom
fix-parse-edge-cases
Nov 26, 2026
Merged

peterhellberg merged 4 commits into
masterfrom
fix-parse-edge-cases

Conversation

@peterhellberg

Copy link
Copy Markdown
Owner

Parse converted each component with strconv.ParseFloat and fmt.Sprintf("%f") before calling time.ParseDuration. That rounding lost precision and left several edge cases. This replaces it with exact integer arithmetic: components are summed as 128-bit fixed-point nanoseconds and rounded once, with halves rounded away from zero.

Fixes

  • Values were rounded to microseconds, so PT0.0000001S parsed as 0s; it now gives 100ns
  • Year, month, week, day and fractional hour values were off by up to a few milliseconds, e.g. P1M was 2629743.0012s instead of 2629743.00012s
  • The exact largest and smallest time.Duration values could not be parsed
  • -P and -PT returned (0, nil); they now return ErrInvalidString
  • , is now accepted as a decimal separator, as ISO 8601 allows (PT1,5S)
  • Inputs over 256 bytes are rejected before matching, since parsing time grew with input length (about 1.7 s for 16 MB)

Parsing is about three times faster, and allocations dropped from 14 to 2 per call.

Behaviour changes

The exported API is unchanged, but some results differ from v0.0.3:

  • Durations with years, months, weeks, days or fractional hours may differ by up to a few milliseconds, because the old values were wrong
  • -P and - now return ErrInvalidString
  • Fractional digits after the 18th are ignored

I compared about 200,000 generated inputs against v0.0.3. Every difference falls into the categories above, and every result matches an exact reference calculation written with math/big. Because results change, I suggest tagging this as v0.1.0.

Tests and docs

  • The tests now use subtests, compare time.Duration values and check errors with errors.Is; TestCompareWithTimeParseDuration no longer ignores errors
  • New tests cover precision and rounding, the largest and smallest values, the comma separator, -P/-PT/-, months vs minutes, and the length limit
  • New FuzzParse checks the error contract, that the sign of the result matches the input, and that adding - negates the result exactly
  • The package doc and README now describe the format as ISO 8601 (RFC 3339 Appendix A), list what's accepted beyond the strict grammar, and use go get …@latest

go vet and go test -race pass with 100% coverage on Go 1.27.4, and fuzzing found no failures.

@peterhellberg peterhellberg self-assigned this Nov 26, 2026
@peterhellberg
peterhellberg merged commit cdc3e37 into master Nov 26, 2026
3 checks passed
@peterhellberg
peterhellberg deleted the fix-parse-edge-cases branch November 26, 2026 08:20
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.

1 participant