Interval

The Interval module provides a type for representing time intervals (ranges between two timestamps). Useful for scheduling, date ranges, and temporal queries.

Creation

Interval.new
Int -> Int -> Interval
Creates a new interval from start and end timestamps (in milliseconds).
start = Time.parse "2024-01-01" "%Y-%m-%d"
end = Time.parse "2024-01-31" "%Y-%m-%d"
january = Interval.new start end
println (Interval.days january)
# => 30

Accessors

Interval.start
Interval -> Int
Returns the start timestamp of the interval.
Interval.end
Interval -> Int
Returns the end timestamp of the interval.
Interval.duration
Interval -> Duration
Returns the duration of the interval.
interval = Interval.new start end
dur = Interval.duration interval
println (Duration.to-hours dur)
Interval.days
Interval -> Int
Returns the number of whole days in the interval.

Set Operations

Interval.intersection
Interval -> Interval -> Option Interval
Returns the intersection of two intervals, or None if they don't overlap.
a = Interval.new 0 100
b = Interval.new 50 150
match Interval.intersection a b
  | Some i -> println "Overlap: ${Interval.start i} to ${Interval.end i}"
  | None -> println "No overlap"
# => "Overlap: 50 to 100"
Interval.union
Interval -> Interval -> Interval
Returns the union (combined span) of two intervals.
a = Interval.new 0 100
b = Interval.new 50 150
combined = Interval.union a b
println "${Interval.start combined} to ${Interval.end combined}"
# => "0 to 150"
Interval.gap
Interval -> Interval -> Option Interval
Returns the gap between two non-overlapping intervals, or None if they overlap.
a = Interval.new 0 50
b = Interval.new 100 150
match Interval.gap a b
  | Some g -> println "Gap: ${Interval.start g} to ${Interval.end g}"
  | None -> println "No gap"
# => "Gap: 50 to 100"

Predicates

Interval.contains?
Interval -> Int -> Bool
Returns true if the interval contains the given timestamp.
interval = Interval.new 0 100
println (Interval.contains? interval 50)
# => true
println (Interval.contains? interval 150)
# => false
Interval.contains-interval?
Interval -> Interval -> Bool
Returns true if the first interval fully contains the second.
outer = Interval.new 0 100
inner = Interval.new 25 75
println (Interval.contains-interval? outer inner)
# => true
Interval.overlaps?
Interval -> Interval -> Bool
Returns true if two intervals overlap.
a = Interval.new 0 100
b = Interval.new 50 150
println (Interval.overlaps? a b)
# => true
Interval.adjacent?
Interval -> Interval -> Bool
Returns true if two intervals are adjacent (end of one equals start of other).
a = Interval.new 0 50
b = Interval.new 50 100
println (Interval.adjacent? a b)
# => true
Interval.valid?
Interval -> Bool
Returns true if the interval is valid (start <= end).
Interval.empty?
Interval -> Bool
Returns true if the interval is empty (start == end).

Manipulation

Interval.shift
Interval -> Int -> Interval
Shifts the interval by the given number of milliseconds.
interval = Interval.new 0 100
shifted = Interval.shift interval 50
println "${Interval.start shifted} to ${Interval.end shifted}"
# => "50 to 150"
Interval.extend
Interval -> Int -> Interval
Extends the interval's end by the given number of milliseconds.
interval = Interval.new 0 100
extended = Interval.extend interval 50
println (Interval.end extended)
# => 150