Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an overlapping dates function to Timex.Interval #669

Open
threeaccents opened this issue May 19, 2021 · 0 comments
Open

Adding an overlapping dates function to Timex.Interval #669

threeaccents opened this issue May 19, 2021 · 0 comments

Comments

@threeaccents
Copy link

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants