Skip to content

fix: LocalDateTimeRange.Split reaches the end when a nanosecond period's product overflows - #262

Merged
CaffeinatedCoder merged 2 commits into
mainfrom
fix/255-localdatetime-split-overflow
Oct 19, 2026
Merged

CaffeinatedCoder merged 2 commits into
mainfrom
fix/255-localdatetime-split-overflow

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #255

Problem

#226 (#219) gave the step-product fallback to Int64Range, DecimalRange and the float ranges, and missed LocalDateTimeRange.Split(Period). NodaPeriods.Times computes checked(period.Nanoseconds * n), which leaves long while the boundary is still millennia inside the domain, and RangeSplitting.Steps read the overflow as the end of the domain. [0001-01-01T00:00, 9001-01-01T00:00).Split(Period.FromNanoseconds(1e18)) gave 10 pieces, the last about 8,700 years wide. #226's PR said the NodaTime steps couldn't overflow on the product. That was true for Duration, ticks, days and months, but not for a period's nanoseconds.

Change

  • LocalDateTimeRange.Split(Period) passes previous => RangeArithmetic.Checked(() => previous.Plus(step)) as the fallback. Adding nanoseconds to a LocalDateTime is exact, so the boundaries are the same as start + n × step wherever that fits.
  • Every other Split/Values(step) call site checked. A product can overflow only if n × step passes its type's limit while start + n × step is still inside the range. Each site's product type is listed with its limit:
Call site Product Why it can't overflow first
Int32Range n × step in long an int range is far below long
Int64Range, DecimalRange, Float64Range, Float32Range — have the fallback (#226)
DateRange Split/Values days, int a boundary inside the range is at most 3.65 million days from the start
DateTimeRange, DateTimeOffsetRange, TimeRange ticks, long the whole DateTime domain is 3.2e18 ticks, so n × step inside the range is at most twice that
InstantRange Duration Duration spans about 45,900 years, and the Instant domain about 20,000
LocalDateRange Split/Values Period days/weeks/months/years, int bounded by the range as for DateRange. A time unit throws ArgumentException at the first boundary
YearMonthRange Split/Values months, int at most 240,000 months
LocalDateTimeRange Period nanoseconds, long can overflow: the domain is 6.3e20 ns. Fixed here. Ticks and milliseconds stay below long inside the range
  • No changelog line: Split is new in 10.0.0.

Tests

  • RangeSplitOracleTests.Split_LocalDateTimeByANanosecondPeriod_ReachesTheEnd is the issue's case. Every piece starts one step after the previous one's start, and the last is at most one step long. Claim R35 names it.
  • The revert is the seed split-nanosecond-period-ends-early.patch ("10 pieces, the last starting at 0286-03-14 16:00", caught: dotnet run tools/oracles.cs seeds split-nanosecond).
  • dotnet test: unit 1374, NodaTime 236, conventions 400, all green.

🤖 Generated with Claude Code

…d's product overflows

The #219 fix gave the step-product fallback to Int64Range, DecimalRange
and the float ranges, and missed LocalDateTimeRange: a Period's
nanosecond count times n leaves long while the boundary is still
millennia inside the domain, NodaPeriods.Times throws, and Steps read
that as the end of the domain. Split(Period.FromNanoseconds(1e18)) over
nine thousand years gave ten pieces, the last 8,700 years wide.

LocalDateTimeRange now passes previous.Plus(step) as the fallback. Every
other Split and Values(step) call site was checked: Int32Range computes
in long, and the day, month, tick and Duration products cannot pass
their type's limit before the boundary passes the range's end, so none
needs one.

Closes #255
Found-By: CA
Defect-Class: ALG

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 19, 2026
@CaffeinatedCoder CaffeinatedCoder added bug Something isn't working silent Answers wrongly without throwing — fixes go in the next major, never deferred area/ranges Range unions, variants and the shape engines package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/medium Inconsistency or failure in a narrow scenario labels Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/ranges Range unions, variants and the shape engines bug Something isn't working package/nodatime Affects CodoMetis.ValueRanges.NodaTime severity/medium Inconsistency or failure in a narrow scenario silent Answers wrongly without throwing — fixes go in the next major, never deferred

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LocalDateTimeRange.Split(Period) still ends early when a nanosecond period's product overflows

1 participant