Skip to content

feat: calendar factories (Month, Quarter, Year, IsoWeek) for the date ranges - #190

Merged
CaffeinatedCoder merged 3 commits into
mainfrom
feat/48-calendar-factories
Oct 18, 2026
Merged

CaffeinatedCoder merged 3 commits into
mainfrom
feat/48-calendar-factories

Conversation

@CaffeinatedCoder

@CaffeinatedCoder CaffeinatedCoder commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

Closes #48

Problem

A month, a quarter, a year or a week was built by hand with CreateFinite and the right last day. That is easy to get wrong in February, at a quarter's end, and for weeks, where the first day and the numbering depend on a convention. The names and the week rule are hard to change later, and 10.0.0 is the LTS line, so they had to be settled before shipping.

Change

Type New static members
DateRange Month(int year, int month), Month(DateOnly), Quarter(int year, int quarter), Quarter(DateOnly), Year(int year), Year(DateOnly), IsoWeek(int weekYear, int week), IsoWeek(DateOnly)
LocalDateRange the same, over LocalDate
YearMonthRange Quarter(int, int), Quarter(YearMonth), Year(int), Year(YearMonth)

YearMonthRange has no Month (a one-month range is CreateFinite(m, m)) and no IsoWeek (a week does not align to months).

Naming rationale (permanent on the LTS line)

  • The period as a noun: DateRange.Month(2026, 10), not CreateMonth or ForMonth. The call reads as the value it returns. The noun can't be mistaken for a shape factory: CreateFinite, CreateUnboundedStart and CreateUnboundedEnd name a shape, and these name a calendar period. That matches .NET's own value factories (TimeSpan.FromDays, NodaTime's Period.FromMonths) and the issue's sketch. No range type has an instance member of these names, and none can get one later without a clash the compiler reports.
  • IsoWeek, not Week. The rule is part of the name, so every call site states it, and nobody expects the culture's week. It also follows .NET's System.Globalization.ISOWeek. A culture-dependent week will never be added, by the decision on this issue. If one ever were, Week would still be free, but nobody would be misled by IsoWeek meanwhile.
  • weekYear, not year, for the week's year. Near January 1 the ISO week-year differs from the calendar year: IsoWeek(2026, 53) holds 2027-01-01. NodaTime's own name (WeekYearRules, GetWeekYear) says that, where .NET's ISOWeek calls it year.
  • Two overloads per period: the numbers, or any day inside it. "The month holding today" is the commonest use. A separate MonthOf(date) name would double the surface for no gain in clarity.
  • They return the union type (DateRange), not the Finite variant. I tried Finite first, for .Start and .End without a pattern match. It made the TRange half of the algebra (Merge, Except, IsAdjacentTo, Union, Gap) uncallable without a cast, because those extensions need TRange : IRangeFactory<TRange, T>, which a variant is not. Same return type as CreateFinite.

Week rule

ISO 8601 only, as decided: Monday to Sunday, and week 1 holds the year's first Thursday. The BCL side uses System.Globalization.ISOWeek and day arithmetic, and the NodaTime side uses WeekYearRules.Iso. The current culture's FirstDayOfWeek and calendar are never read. #100 (Split) uses the same rule.

Finding

ISOWeek.ToDateTime(2027, 53, Monday) does not throw. It returns 2028-01-03, the Monday of week 1 of 2028, because .NET checks week <= 53 rather than against the week-year. So IsoWeek(2027, 53) would have answered silently with the wrong week. DateRange.IsoWeek now checks ISOWeek.GetWeeksInYear and throws ArgumentOutOfRangeException, as NodaTime does. The revert is kept as the seed iso-week-past-the-last-wraps.patch.

EF Core

Called with values the query does not read, a factory is evaluated in memory and inlined as one daterange constant. So b.Period.Overlaps(DateRange.Month(2026, 10)) runs as b."Period" && '[2026-10-01,2026-10-31]'::daterange. Called with a column, it fails translation in a Where and runs in memory in a Select. That is listed in docs/efcore.md#what-runs-where, and its intro now reads "almost always because PostgreSQL has no operator". Translating the column form through date_trunc and make_date is a follow-up (#191).

Also

The calendar block sits beside Values() rather than after CreateFinite. Inserted after CreateFinite, it shifted the context of the existing seed daterange-reports-continuous.patch, which then failed EverySeed_AppliesToTheCurrentTree.

Docs: docs/ranges.md ("Create a month, a quarter, a year or a week"), the efcore row, package READMEs, changelogs under 10.0.0. Claims R32–R34 and E46. 13 clone-sweep waivers for the members only the date ranges have.

Tests

  • New oracle CalendarRangeParityTests (conventions, differential, registered). It checks every day from 1999-12-01 to 2031-01-31 plus 14 days at each end of the domain, under the invariant culture, en-US, fa-IR, de-DE and ar-EG. Each day is checked against the definitions (the period holds the day, the week runs Monday to Sunday) and against .NET's ISOWeek numbering. The LocalDateRange and YearMonthRange results must match DateRange's. Months and quarters must partition their year, consecutive ISO weeks must be adjacent across week-years, and week 0 or week n+1 must throw in both packages.
  • Seeds, both caught (dotnet run tools/oracles.cs seeds iso-week): iso-week-reads-the-culture.patch (the week starts on the culture's first day) and iso-week-past-the-last-wraps.patch (the finding above).
  • CalendarRangeTests, NodaTimeCalendarRangeTests: documented examples, argument checks, domain edges, and a Julian LocalDate read as its ISO day.
  • EF: InlineRangeConstantTests (the constant SQL), ClientSideOnlyTranslationTests (a column argument fails in Where), and ClientSideOnlyProjectionTests (integration: a column argument in Select, and the constant form in Where, against PostgreSQL).
  • dotnet test: unit 1030, NodaTime 200, EF translation 254 + 81, conventions 326, ASP.NET Core 84, all green.

🤖 Generated with Claude Code

…ange

DateRange.Month, Quarter, Year and IsoWeek build the calendar ranges
people build by hand, by number or from any day inside the period;
LocalDateRange gets the same, YearMonthRange gets Quarter and Year.
Weeks are ISO 8601 only: Monday to Sunday, numbered by ISO week-year,
never the current culture's first day of the week. Each returns the
union type, like CreateFinite, so the whole algebra applies without a
cast.

DateRange.IsoWeek(weekYear, week) checks the week against the
week-year's length: ISOWeek.ToDateTime reads week 53 of a 52-week year
as week 1 of the next, silently.

CalendarRangeParityTests holds both packages against .NET's ISOWeek and
NodaTime's WeekYearRules.Iso for every day of 1999-2031 and both domain
ends, under five current cultures; two seeds prove it.

Closes #48

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 enhancement New feature or request area/ranges Range unions, variants and the shape engines package/nodatime Affects CodoMetis.ValueRanges.NodaTime package/core Affects CodoMetis.ValueRanges labels Oct 18, 2026
# Conflicts:
#	CHANGELOG.md
#	docs/efcore.md
#	src/CodoMetis.ValueRanges.NodaTime/CHANGELOG.md
#	src/CodoMetis.ValueRanges/CHANGELOG.md
#	test/CodoMetis.ValueRanges.EFCore.PostgreSQL.IntegrationTests/ClientSideOnlyProjectionTests.cs
#	test/CodoMetis.ValueRanges.EFCore.PostgreSQL.Tests/ClientSideOnlyTranslationTests.cs
#	test/oracles/claims.json
#	test/oracles/clone-waivers.json
#	test/oracles/oracles.json
# Conflicts:
#	test/oracles/claims.json
@CaffeinatedCoder
CaffeinatedCoder merged commit 7033499 into main Oct 18, 2026
@CaffeinatedCoder
CaffeinatedCoder deleted the feat/48-calendar-factories branch October 18, 2026 18:05
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 enhancement New feature or request package/core Affects CodoMetis.ValueRanges package/nodatime Affects CodoMetis.ValueRanges.NodaTime

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Calendar factories for DateRange (month, quarter, year, week)

1 participant