Skip to content

Validate interval bounds before zero-time shortcuts - #406

Merged
gouguoyin merged 2 commits into
dromara:masterfrom
anishpentyala:fix-zero-interval-bounds-1
Nov 23, 2026
Merged

gouguoyin merged 2 commits into
dromara:masterfrom
anishpentyala:fix-zero-interval-bounds-1

Conversation

@anishpentyala

Copy link
Copy Markdown
Contributor

BetweenIncludedStart, BetweenIncludedEnd and BetweenIncludedBoth can 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:

carbon.NewCarbon().BetweenIncludedStart(carbon.NewCarbon(), nil) // true before this change

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:

  • Go 1.22.12: GOGC=20 go test -p 1 -count=1 ./..., all six packages pass, including a fresh clone of this branch.
  • Go 1.22.12: 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.
  • Go 1.18.10: GOGC=20 go test -gcflags=all=-l -p 1 -count=1 ./..., all six packages pass.
  • Go 1.22.12: 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.

@gouguoyin
gouguoyin merged commit 5d87fd7 into dromara:master Nov 23, 2026
7 checks passed
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.
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