Skip to content

fix: month shifts move a date range's exclusive end, and Shift keeps infinite bounds - #238

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
fix/223-224-shift-calendar-and-infinity
Oct 18, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
fix/223-224-shift-calendar-and-infinity

Conversation

@CaffeinatedCoder

Copy link
Copy Markdown
Owner

Closes #223
Closes #224

Two commits, one per issue. They touch the same Shift/Expand code, so they come together.

Problem

Change

#223 (commit 1): LocalDateRange moves its upper bound as the exclusive end PostgreSQL stores (EndMoved: the day after, plus the period, less a day), as decided on the issue.

  • Month(y, m).Shift(P1M) == Month(y, m + 1), the shift back restores it, and a start on the 31st still clips.
  • I checked the other closed-end date types. DateRange steps by days and YearMonthRange by whole months, and there moving the end and moving the day after it agree, so neither changes. Both are pinned: YearMonthRange.Quarter(…).Shift(3) is the next quarter.

#224 (commit 2): RangeArithmetic.MoveKeepingInfinity leaves a bound that stands for an infinity where it is, as decided on the issue. Each side has its own test, per type:

Type Bounds that stay
DateTimeRange, DateTimeOffsetRange, LocalDateTimeRange, InstantRange MinValue / MaxValue (MinIsoValue / MaxIsoValue)
YearMonthRange its first and last ISO month, which the store writes as the date extremes
DateRange, LocalDateRange the extremes, and a closed end the day before the maximum
  • Why the day before the maximum. A closed date range's end stands for infinity) there, because its exclusive end is the extreme: a column holding [x,infinity) loads as [x,9999-12-30] (A discrete range read as [x,infinity) loses 9999-12-31; UpperBound disagrees between memory and server #117). That is the issue's first example. The lower side has no such rule, because PostgreSQL's canonical lower bound is inclusive, so 0001-01-02 is a real date.
  • A finite bound moved past an extreme still throws OverflowException. The integer, float and TimeRange types are unchanged.
  • #223's EndMoved never sees the calendar's last day now, so its special case is gone.

What the live proof can and can't show

A daterange with an exclusive infinity) already comes back finite from a plain save, moved or not. It loads as [x,9999-12-30], and the plugin writes that as [x,9999-12-31). #117 settled that by documenting it, since reading it the other way broke the server's own algebra. So for that one form, the live test holds what the rule can promise: the moved row saves exactly as the unmoved row does. Every other form (tsrange/tstzrange [x,infinity), daterange [x,infinity] and [-infinity,x], and the NodaTime columns) is shown to still hold infinity after it is shifted and saved. docs/efcore.md says this.

One consequence to know: with Npgsql.DisableDateTimeInfinityConversions set, the extremes are real dates on the server, and the rule still keeps them. The decision names the types, not the switch, so I applied it unconditionally. The InfinityDisabled integration project passes.

Docs and catalogue

  • docs/ranges.md: the month bullet is rewritten, and a new "A bound that stands for infinity stays where it is" bullet is added.
  • docs/efcore.md: the round trip, next to the A discrete range read as [x,infinity) loses 9999-12-31; UpperBound disagrees between memory and server #117 paragraph.
  • Claims: R31 is rewritten to the new month rule, R30's text now refers to the new R42, and R42 holds the infinity rule.
  • Changelogs: Shift and Expand are new in 10.0.0, so these are fixes to something new in the same release, and CONTRIBUTING leaves those out. I refined the existing Added lines (root, core, NodaTime) to state both rules instead.
  • Five clone-sweep waivers: EndMoved and EndIsInfinity, the InstantRange and YearMonthRange extreme spellings.

Tests

  • LocalDateRange Shift/Expand by months move the closed last day, so Month(2026,2).Shift(P1M) is not March #223: NodaTimeShiftExpandTests checks every month of 2024 and 2026, both directions with a round trip, every quarter by P3M, a year by P1Y, two Expand cases, the clipping start, and YearMonthRange quarters. With the old rule restored (end.Plus(delta)), three tests fail.
  • Shift/Expand turn a bound that stands for ±infinity into a finite date #224:
    • RangeArithmeticOracleTests' edge rows: on the seven types whose extremes stand for an infinity, the extreme bound stays under Shift(±1) and Expand(±1), and Shift(1).Shift(-1) is the identity. A finite bound two steps below the maximum may move one step and throws at three.
    • New InfinityShiftRoundTripTests (integration, live): six cases, described above.
    • Seed shift-moves-an-infinite-bound.patch is caught on all seven types (dotnet run tools/oracles.cs seeds shift). With the seed applied, all six live tests fail too.
    • RangeShiftExpandTests.Shift_ABoundThatStandsForInfinity_Stays. The two existing overflow asserts at DateOnly.MaxValue and DateTime.MaxValue now start a finite bound below the extreme.
  • dotnet test: unit 1346, NodaTime 236, EF translation 284 + 83, ASP.NET Core 92, conventions 395, integration 262, InfinityDisabled 4, all green.

🤖 Generated with Claude Code

CaffeinatedCoder and others added 2 commits October 18, 2026 21:28
LocalDateRange.Shift and Expand by a period of months moved the closed
last day, which NodaTime clips to a shorter month: Month(2026, 2)
shifted by a month was March 1 to 28, and Quarter(2026, 3) by three
months ended on December 30. LocalDateTimeRange [2026-02-01,2026-03-01)
and PostgreSQL's daterange of the same month both move to all of March.

The upper bound now moves as the day after it, the exclusive end
PostgreSQL stores, and steps back a day, as the maintainer decided on
#223: Month(y, m).Shift(P1M) is Month(y, m + 1), and the shift back
restores it. A start on the 31st still clips. DateRange steps by days
and YearMonthRange by whole months, where moving the end and moving
the day after it agree, so neither changes.

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

Co-Authored-By: Claude Opus 5.5 <[email protected]>
On the temporal ranges the extremes stand for PostgreSQL's -infinity and
infinity: Npgsql reads infinity as DateTime.MaxValue, DateOnly.MaxValue,
LocalDate.MaxIsoValue and the rest, and writes them back as infinity.
Shift and Expand moved such a bound like any other value, so a tsrange
read from [2026-01-01,infinity), shifted back a day and saved, ended on
9999-12-30 23:59:59.999999, where PostgreSQL's upper(r) - 1 keeps
infinity, and a bound at an extreme threw where infinity + 1 does not.

As the maintainer decided on #224, a bound at an extreme of DateRange,
DateTimeRange, DateTimeOffsetRange, LocalDateRange, LocalDateTimeRange,
InstantRange or YearMonthRange now stays where it is, and so does a
closed date range's end the day before the maximum, whose exclusive end
the extreme is (a column holding [x,infinity) loads as [x,9999-12-30]).
A finite bound moved past an extreme still throws, and the integer,
float and time ranges are unchanged.

RangeArithmeticOracleTests holds the rule at the edges of every
infinity-backed type; InfinityShiftRoundTripTests shows it live for
tsrange, tstzrange, daterange and the NodaTime columns. The seed is
caught by both.

Closes #224
Found-By: CA
Defect-Class: STORE

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@CaffeinatedCoder CaffeinatedCoder added this to the 10.0.0 milestone Oct 18, 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 package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL package/core Affects CodoMetis.ValueRanges severity/medium Inconsistency or failure in a narrow scenario labels Oct 18, 2026
# Conflicts:
#	test/CodoMetis.ValueRanges.Tests/RangeShiftExpandTests.cs
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/core Affects CodoMetis.ValueRanges package/efcore Affects CodoMetis.ValueRanges.EFCore.PostgreSQL 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

1 participant