C# Library to handle intervals (composite start and end)
- Targets
.NET Standard 2.0for wide compatibility. - Support for generic types in intervals (e.g.
DateTimeOffset,int,double). - Support for open and closed intervals.
- Support for common operations like Covers(), Intersect(), Join(), etc. via extension methods.
- Support for disjoint collections of intervals via
DisjointIntervalSet. - Makes use of Optional to avoid returning
nullin many of the built-in operations.
A new version (1.0) of this library including generic intervals has been released. See more info at Changelog (or CHANGELOG.md).
The documentation is split into four core sections:
- Basic Intervals: Introduction to
IBasicInterval<TBoundary>,BasicInterval<TBoundary>, and boundary methods (Covers,Intersects,Intersect,Join,IsContiguouslyFollowedBy,StartsBefore, etc.). - Full Intervals: Introduction to
FullInterval<TBoundary, TLength>, metric measurement viaILengthOperator, standard classes (DateTimeOffsetInterval,IntInterval,DoubleInterval,DateOnlyInterval,TimeOnlyInterval), and all length methods (DefaultMeasure,Shift,Subtract,Partition, etc.). - Interval Sets: Management of ordered, non-overlapping collections via
DisjointIntervalSet<TBoundary, TLength>, set consolidation, and set arithmetic. - Custom Intervals: Comprehensive guide and architectural considerations for creating custom interval types and custom length operators.
You can also check the Methods Reference.
The API-documentation is hosted at albertogregorio.com/ephemeral.
Starting from version 1, Ephemeral introduces generic intervals allowing boundaries of any type that implements IComparable<TBoundary>. It defines two primary categories of intervals:
- Basic intervals (
IBasicInterval<TBoundary>,BasicInterval<TBoundary>): Work with any comparable type without requiring length metrics or arithmetic operators. They support boundary-based operations such as containment (Covers), overlaps (Intersects,Intersect), contiguous checks (IsContiguouslyFollowedBy,IsContiguouslyPrecededBy), and ordering (StartsBefore). - Full intervals (
FullInterval<TBoundary, TLength>,IHasLength<TLength>): Extend basic intervals by incorporating length measurement (TLength) via anILengthOperator<TBoundary, TLength>. They support length-dependent operations such asDefaultMeasure(), shifting (Shift,ShiftStart,ShiftEnd), partitioning (Partition), intersection length (LengthOfIntersect), and subtraction (Subtract).
To facilitate migration, Ephemeral provides several utility classes corresponding to standard types that extend FullInterval:
DateTimeOffsetInterval(forDateTimeOffset, measured withTimeSpan)IntInterval(forint, measured withint)DoubleInterval(fordouble, measured withdouble)DateOnlyInterval(forDateOnly, measured in days asint, available in .NET 6+ targets)TimeOnlyInterval(forTimeOnly, measured withTimeSpan, available in .NET 6+ targets)
In Ephemeral, intervals are parameterized by both their boundary type (TBoundary) and their measurement type (TLength). The length represents the difference or distance between the start and end of an interval.
Utility classes extending FullInterval<TBoundary, TLength> implement IHasLength<TLength> and provide standard length calculations out of the box:
DoubleInterval: boundarydouble, lengthdouble(difference between end and start).IntInterval: boundaryint, lengthint(difference between end and start).DateTimeOffsetInterval: boundaryDateTimeOffset, lengthTimeSpan(time duration).DateOnlyInterval: boundaryDateOnly, lengthint(number of days).TimeOnlyInterval: boundaryTimeOnly, lengthTimeSpan(elapsed time).
You can obtain the length of a full interval using DefaultMeasure():
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
A.DefaultMeasure() -> 4
var interval = DoubleInterval.CreateClosed(2.0, 6.0);
double length = interval.DefaultMeasure(); // returns 4.0
var degenerate = DoubleInterval.CreatePoint(42.0);
double pointLength = degenerate.DefaultMeasure(); // returns 0.0For basic intervals (IBasicInterval<TBoundary>), length can be measured explicitly by providing an ICanMeasure<TBoundary, TLength> or ILengthOperator<TBoundary, TLength> via Measure() or WithMeasure():
var basic = new BasicInterval<double>(2.0, 6.0, true, true);
double length = basic.Measure(DoubleDefaultLengthOperator.Instance); // returns 4.0Length (TLength) is utilized across various Ephemeral operations:
- Shifting boundaries (
Shift,ShiftStart,ShiftEnd): Offsets of typeTLengthare applied to interval boundaries using the interval'sILengthOperator<TBoundary, TLength>. - Intersection length (
LengthOfIntersect): Calculates theTLengthof the overlapping region between two intervals, returning zero if they do not intersect. - Interval sets (
DisjointIntervalSet): Collections of disjoint intervals use anILengthOperator<TBoundary, TLength>to compute bounding intervals (GetBoundingInterval()), apply offsets, and manage unions and subtractions.
Here are graphical representations of common interval operations over the real line (using DoubleInterval). Operations that only depend on boundary comparisons require a basic interval (IBasicInterval<TBoundary>), while operations that translate boundaries or compute metrics require a full interval (FullInterval<TBoundary, TLength>) or an explicit ILengthOperator.
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if an interval completely contains another interval (or point).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======================] (A = [2, 8])
Interval B: [=======] (B = [4, 6])
A.Covers(B) -> true
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if two intervals overlap. Intersect() returns the overlapping interval.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
Interval B: [===============] (B = [4, 8])
A.Intersects(B) -> true
A.Intersect(B) -> [=======] ([4, 6])
Requires: Basic interval (IBasicInterval<TBoundary>)
Merges two overlapping or contiguous intervals into a single interval.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
Interval B: [===============] (B = [4, 8])
A.Join(B) -> [=======================] ([2, 8])
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit ILengthOperator
Subtracts one interval from another, returning a disjoint set of intervals.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======================] (A = [2, 8])
Interval B: [=======] (B = [4, 6])
A.Subtract(B) -> [=======) (=======] ([2, 4) and (6, 8])
Requires: Basic interval (IBasicInterval<TBoundary>) with an explicit ILengthOperator
Combines two intervals into a collection (DisjointIntervalSet).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======] (A = [2, 4])
Interval B: [=======] (B = [6, 8])
A.Union(B) -> [=======] [=======] ([2, 4] and [6, 8])
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if one interval starts exactly where another ends (without overlap, typically open/closed).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======) (A = [2, 4))
Interval B: [=======] (B = [4, 6])
A.IsContiguouslyFollowedBy(B) -> true
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if one interval ends exactly where another starts (without overlap, typically open/closed).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======] (A = [4, 6])
Interval B: [=======) (B = [2, 4))
A.IsContiguouslyPrecededBy(B) -> true
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if an interval starts before another interval.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======] (A = [2, 4])
Interval B: [=======] (B = [4, 6])
A.StartsBefore(B) -> true
Requires: Basic interval (IBasicInterval<TBoundary>)
Checks if two intervals are equivalent (have the same boundaries and inclusion rules).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======] (A = [2, 4])
Interval B: [=======] (B = [2, 4])
Interval C: [=======) (C = [2, 4))
A.IsEquivalentIntervalTo(B) -> true
A.IsEquivalentIntervalTo(C) -> false
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit ILengthOperator
Shifts an interval by a given duration/offset.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======] (A = [2, 4])
A.Shift(4) -> [=======] ([6, 8])
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit ILengthOperator
Shifts the start of an interval by a given duration/offset.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
A.ShiftStart(2) -> [=======] ([4, 6])
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit ILengthOperator
Shifts the end of an interval by a given duration/offset.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
A.ShiftEnd(2) -> [=======================] ([2, 8])
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit ILengthOperator
Splits an interval into contiguous sub-intervals of a specified length (DisjointIntervalSet). Intermediate boundaries can be configured with intermediateEndsClosed (defaults to false, generating [start, end) intermediate intervals).
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [=======================] (A = [2, 8])
A.Partition(2) -> [=======) ([2, 4))
[=======) ([4, 6))
[=======] ([6, 8])
Requires: Full interval (FullInterval<TBoundary, TLength>), or a basic interval with an explicit measurer (ICanMeasure / ILengthOperator)
Calculates the length of the intersection between two intervals.
Real Line: 0---1---2---3---4---5---6---7---8---9---10
Interval A: [===============] (A = [2, 6])
Interval B: [===============] (B = [4, 8])
A.LengthOfIntersect(B) -> 2
var now = DateTimeOffset.UtcNow;
var yesterday = DateTimeOffsetInterval.CreateOpen(now.AddDays(-1), now);
var today = yesterday.Shift(TimeSpan.FromDays(1));
yesterday.Intersects(today); // returns false (because they are open/closed contiguously)var firstTen = IntInterval.CreateClosed(1, 10);
var nextTen = IntInterval.CreateClosed(11, 20);
firstTen.IsContiguouslyFollowedBy(nextTen); // returns false if both are closed.var range1 = DoubleInterval.CreateClosed(1.5, 4.5);
var range2 = DoubleInterval.CreateOpen(3.0, 6.0);
range1.Intersects(range2); // returns true
range1.Covers(4.5); // returns true
range2.Covers(3.0); // returns false (because it's open)Basic intervals work with any type implementing IComparable<T>, without requiring length operators or metrics:
var stringInterval = new BasicInterval<string>("apple", "orange", true, true);
var otherStringInterval = new BasicInterval<string>("banana", "pear", true, true);
stringInterval.Covers("cherry"); // returns true
stringInterval.Intersects(otherStringInterval); // returns truevar lengthOperator = DateTimeOffsetTimeSpanLengthOperator.Instance;
var collection = new DisjointIntervalSet<DateTimeOffset, TimeSpan>(lengthOperator);
collection.Add(yesterday);
collection.Add(today);
collection.Start == yesterday.Start; // true
collection.End == today.End; // true
var collection2 = yesterday.Union(today, lengthOperator);
var consolidatedCollection = collection2.Consolidate();
collection2.Count(); // 2
consolidatedCollection.Count(); // 1var start = new DateOnly(2023, 1, 1);
var end = new DateOnly(2023, 12, 31);
var year = DateOnlyInterval.CreateClosed(start, end);
var other = DateOnlyInterval.CreateClosed(new DateOnly(2023, 6, 1), new DateOnly(2023, 8, 31));
year.Covers(other); // returns truevar morningStart = new TimeOnly(8, 0);
var morningEnd = new TimeOnly(12, 0);
var morning = TimeOnlyInterval.CreateClosed(morningStart, morningEnd);
var workDay = TimeOnlyInterval.CreateClosed(new TimeOnly(8, 0), new TimeOnly(17, 0));
workDay.Covers(morning); // returns trueAn Interval Set (DisjointIntervalSet<TBoundary, TLength>) represents an ordered collection of mutually disjoint (non-overlapping) intervals. It enforces that contained intervals cannot overlap (throwing OverlapException if an overlap is introduced) and automatically maintains intervals sorted by their start boundary.
Interval sets are ideal for managing discontinuous ranges, such as available calendar time slots, non-overlapping shifts, or fragmented numeric intervals resulting from subtractions.
Key capabilities include:
- Consolidation: Merging contiguous adjacent intervals into a minimal continuous set (
Consolidate()). - Set Coverage: Checking whether a point or an entire interval is covered across the set's segments (
Covers()). - Set Intersection & Joins: Intersecting intervals against the set (
Intersect()) or joining new intervals and sets (Join()). - Bounding Interval: Computing the minimal contiguous interval enclosing the entire set (
GetBoundingInterval()).
For the comprehensive guide, invariants, and dedicated pages for each method, see the Interval Sets Documentation.
Starting with version 0.3.0, the Ephemeral package includes AI agent skills following the Agent Skills standard, packaged directly under skills/ within the NuGet package. These skills provide instructions and detection tools to help AI coding assistants and developers discover and migrate interval logic to Ephemeral abstractions.
Located at skills/adapt-to-ephemeral/:
- Purpose: Guides refactoring ad-hoc boundary and range implementations into Ephemeral intervals.
- When to Use:
- Codebases containing paired boundary properties or variables (e.g.,
Start/End,From/To,Min/Max). - Manual range comparison logic (containment checks, overlap tests, interval intersection).
- Custom interval consolidation or merging routines.
- Codebases containing paired boundary properties or variables (e.g.,
- Contents:
SKILL.md: Detailed refactoring patterns mapping common manual interval operations to Ephemeral methods (Covers,Intersects,Intersect,Join,Shift,DisjointIntervalSet).scripts/find-interval-candidates.ps1: A PowerShell script to scan C# source files for potential range variables and candidate patterns for refactoring.
