bitwalker / bitwalker/timex

Adding an overlapping dates function to Timex.Interval

Open
#669 0 comments 1 reaction 0 assignees View on GitHub
feature/enhancement
Dominant language
Elixir
Stars
1.8k
Forks
407
PR merge metrics
No merged PRs in 30d

Description

`Timex.Interval` Has a handy `overlaps?` function that helps us know if two intervals overlap each other. I wanted to propose a new function `overlapping_dates` which creates a new interval with the actual dates that overlap.

Something along the lines of

```elixir
def overlapping_dates(a, b) do
if Timex.Interval.overlaps?(a, b) do
Timex.Interval.new(
from: latest_date(a.from, b.from),
until: earliest_date(a.until, b.until)
)
else
[]
end
end

defp earliest_date(a, b) do
if Timex.before?(a, b) do
a
else
b
end
end

defp latest_date(a, b) do
if Timex.before?(a, b) do
b
else
a
end
end
```

Though it is trivial to create this function with the helpers Timex already provides I thought it would be handy if it was already part of the library.

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.