Skip to content

Interval

The Interval class represents time ranges between two datetime points.

carbonic.core.interval.Interval dataclass

Interval(start: Date | DateTime, end: Date | DateTime)

Represents a time interval with start and end points.

Intervals are half-open: [start, end) - inclusive start, exclusive end. Supports both Date and DateTime intervals with comprehensive operations.

Examples:

Create intervals

meeting = Interval(start=DateTime(2024, 1, 15, 9, 0), end=DateTime(2024, 1, 15, 10, 30)) vacation = Interval(start=Date(2024, 7, 1), end=Date(2024, 7, 15))

Operations

meeting.contains(DateTime(2024, 1, 15, 9, 30)) # True meeting.overlaps(lunch_interval) # True/False meeting.intersection(lunch_interval) # Overlapping part meeting.union(lunch_interval) # Combined intervals meeting.duration() # Duration object

Functions

duration

duration() -> Duration

Get the Duration of this interval.

Returns:

Type Description
Duration

Duration object representing the time span

is_empty

is_empty() -> bool

Check if this interval is empty (zero duration).

Returns:

Type Description
bool

True if start == end, False otherwise

contains

contains(point: Date | DateTime) -> bool

Check if this interval contains a time point.

Uses half-open interval logic: [start, end) - start is inclusive - end is exclusive

Parameters:

Name Type Description Default
point Date | DateTime

Date or DateTime to check

required

Returns:

Type Description
bool

True if point is within the interval

overlaps

overlaps(other: Interval) -> bool

Check if this interval overlaps with another interval.

Parameters:

Name Type Description Default
other Interval

Another Interval to check

required

Returns:

Type Description
bool

True if intervals overlap, False otherwise

intersection

intersection(other: Interval) -> Interval | None

Get the intersection (overlapping part) of two intervals.

Parameters:

Name Type Description Default
other Interval

Another Interval to intersect with

required

Returns:

Type Description
Interval | None

Interval representing the overlap, or None if no overlap

union

union(other: Interval) -> Interval | list[Interval]

Get the union of two intervals.

Parameters:

Name Type Description Default
other Interval

Another Interval to union with

required

Returns:

Type Description
Interval | list[Interval]

Single Interval if they overlap or are adjacent,

Interval | list[Interval]

List of Intervals if they are separate