It seems like it's not possible to create a zero-length interval:
iex(1)> Interval.new(from: ~N[2025-01-01 13:00:00], until: ~N[2025-01-01 13:00:00], left_open: false, right_open: false)
{:error, :invalid_until}
If both left and right ends are not open, it seems technically it should be allowed. However, a zero length interval isn't really useful, so I'm fine with this limitation.
So, I was surprised to find that Interval.difference/2 will create zero-length intervals:
iex(2)> (
Interval.difference(
Interval.new(from: ~N[2025-01-01 12:00:00], until: ~N[2025-01-01 13:00:00], right_open: true),
Interval.new(from: ~N[2025-01-01 12:00:00], until: ~N[2025-01-01 13:00:00], right_open: true)
)
)
[]
iex(3)> (
Interval.difference(
Interval.new(from: ~N[2025-01-01 12:00:00], until: ~N[2025-01-01 13:00:00], right_open: false),
Interval.new(from: ~N[2025-01-01 12:00:00], until: ~N[2025-01-01 13:00:00], right_open: true)
)
)
[
%Timex.Interval{
from: ~N[2025-01-01 13:00:00],
until: ~N[2025-01-01 13:00:00],
left_open: false,
right_open: false,
step: [days: 1]
}
]
If I pass the zero-length interval into |> Interval.duration(:duration) I get back #<Duration(PT0S)>.
It seems like it's not possible to create a zero-length interval:
If both left and right ends are not open, it seems technically it should be allowed. However, a zero length interval isn't really useful, so I'm fine with this limitation.
So, I was surprised to find that
Interval.difference/2will create zero-length intervals:If I pass the zero-length interval into
|> Interval.duration(:duration)I get back#<Duration(PT0S)>.