Skip to content

Adding an overlapping dates function to Timex.Interval #669

Open

Description

@threeaccents

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions