bitwalker / bitwalker/timex

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

Open
#787 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Elixir
Stars
1.8k
Forks
407
PR merge metrics
No merged PRs in 30d

Description

It seems like it's not possible to create a zero-length interval:
```elixir
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:
```elixir
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 `#`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.