Skip to content

Interval.difference/2 can produce intervals not creatable by Interval.new #787

@tisdall

Description

@tisdall

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)>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions