Skip to content

Interested in these Timex.Interval functions? #460

@nathanl

Description

@nathanl

In my work recently on some appointment scheduling code, I've recently written several functions that work with %Timex.Interval{} structs to decide when someone is available for scheduling. I wondered if you'd be interested in having any of them in the library.

Here's what I've written:

  • combine/1 takes a list of intervals and combines any that overlap or touch. In our case, the idea is that if someone has availability from 8-9 and 9-10, it's OK to schedule something from 8:30-9:30; we can combine those two intervals into one.
  • difference/2 is like the set operation - it takes two intervals and removes the second from the first if there's any overlap. It can chop off the beginning or the end, or it can split it into two, so it returns {:ok, list}, which can be empty if it removed the entire interval. We use this for things like "X is available from 8-12 but has an appointment from 9-9:30, so actual availability is 8-9 and 9:30-12."
  • difference_all/2 takes two lists and removes all of the latter from the former. We use this to take all of a person's availability windows for the day and remove all their existing appointments and such, resulting in a list of their remaining availability.
  • list_within/3 lists periods of a given length within an interval. So you can say "list all the possible 1-hour appointments between 10-12, starting every 15 minutes" and get back 10-11, 10:15-11:15, 10:30-11:30, 10:45-11:45, 11-12.

I have tests for all of these.

I was unsure exactly what to do about the other attributes: :step, :left_open and :right_open.
What I do right now is when splitting an interval, both new intervals retain the original's other attributes. When combining intervals, we use the other attributes of the first one. When listing intervals within a container interval, we use the other attributes of the container interval.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions