Skip to content

fix(extremum): preserve subsecond distance ordering - #407

Merged
gouguoyin merged 2 commits into
dromara:masterfrom
anishpentyala:fix-extremum-nanosecond-distance
Nov 23, 2026
Merged

gouguoyin merged 2 commits into
dromara:masterfrom
anishpentyala:fix-extremum-nanosecond-distance

Conversation

@anishpentyala

Copy link
Copy Markdown
Contributor

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.

@gouguoyin
gouguoyin merged commit f31f352 into dromara:master Nov 23, 2026
7 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