Repository navigation
Validate interval bounds before zero-time shortcuts - #406
Merged
gouguoyin merged 2 commits intoNov 23, 2026
Merged
Conversation
Added tests for Carbon date validation with zero and invalid values.
gouguoyin
pushed a commit
that referenced
this pull request
Nov 23, 2026
`Closest` and `Farthest` currently compare `DiffAbsInSeconds`, which truncates distances to whole seconds. This treats distinct fractional distances as ties and keeps the wrong candidate. For a base at `13:14:15.900`, candidates at `.990` and `.910` are 90ms and 10ms away. `Closest(farther, nearer)` currently keeps the 90ms candidate; `Farthest(nearer, farther)` keeps the 10ms candidate. Compare absolute wall-clock distances as seconds plus nanoseconds, normalizing a negative nanosecond remainder by borrowing one second. This retains precision without `time.Duration` saturation on distant dates. True equal-distance ties still keep the first candidate; invalid-input propagation and selected pointer identity are unchanged. Behavior change: cases that were false ties after whole-second truncation now select the actual closest/farthest candidate. This does not change `DiffInSeconds` or `DiffAbsInSeconds`. Their intended whole-second behavior from #379 remains untouched. The interval-bound validation in open #406 is separate; this branch is based on clean upstream master, not that PR. Regression tests cover fractional future/past distances, second-boundary borrowing, opposite sides, nonzero whole-second false ties, one-nanosecond differences and zero distance. Controls cover genuine ties in both orders, different locations and distances beyond `time.Duration`'s range. Validation on Linux/amd64 for the reviewed tree: - Full Go 1.26.6 suite and full race suite, all six packages; vet and build pass. - Full Go 1.18.10 suite with `-vet=off`, matching the existing minimum-version workflow's handling of pre-existing multiple `%w` verbs. Go 1.26.6 vet passes separately. - Test runs used `GOGC=20`, `-p 1` and `-gcflags=all=-l` to reduce compilation memory pressure. The Go 1.26.6 build used ordinary `go build ./...`. - Focused regressions repeated 100 times. New tests fail on unchanged production; four valid precision/tie/normalization mutants fail the assertions. - A separate `math/big` public-API oracle checked 8,000 generated selections for both extrema, 16,000 checks: 6,292 base mismatches and zero candidate mismatches. - Formatting and `git diff --check` pass. No golangci-lint, remote CI, cross-platform or performance result is claimed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BetweenIncludedStart,BetweenIncludedEndandBetweenIncludedBothcan return true when an interval bound is invalid. A zero receiver equal to an included bound takes the zero-time shortcut before validation of the other bound:The same problem occurs with empty and error values, and with the opposite side of the end-inclusive methods.
Move each existing invalid-input check before its zero-time shortcut. The reversed-bound check and valid zero-time behavior stay unchanged, including the existing all-zero half-open interval convention. No interval semantics or other comparison methods are changed.
The regression test covers 756 combinations containing at least one invalid receiver or bound, plus 375 valid comparisons against Go time ordering, including nonzero nanoseconds and equal instants in different locations. On unchanged master, the new tests have 12 assertion failures and 744 passing invalid-input cases. All pass with this change. Restoring each affected method separately produces 3, 3 and 6 matching assertion failures.
Validation on Linux/amd64:
GOGC=20 go test -p 1 -count=1 ./..., all six packages pass, including a fresh clone of this branch.go test -gcflags=all=-l -race -count=1 ./..., all six packages pass. The focused optimized regression target also passes under the race detector for 20 repetitions.GOGC=20 go test -gcflags=all=-l -p 1 -count=1 ./..., all six packages pass.go vet -gcflags=all=-l ./...,go build ./..., formatting and diff checks pass.The compiler settings above reduced memory pressure while compiling the large test package. No remote CI result is claimed.