bitwalker / timex

A complete date/time library for Elixir projects.
https://hexdocs.pm/timex
MIT License
1.75k stars 376 forks source link

Interested in these `Timex.Interval` functions? #460

Open nathanl opened 6 years ago

nathanl commented 6 years ago

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:

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.

bitwalker commented 6 years ago

@nathanl It'd be great to add these capabilities to the Interval API! A few thoughts:

I think difference_all can probably be left out, or built as an overload for difference/2 if the second argument is a list of intervals.

I would probably also change list_within/3 to periods/2 where you specify the interval and the unit/span for each period which should be in the resulting list. That said, list_within/3 could instead just be provided by an implementation of Enumerable which would allow converting a period to a list based on the step of the interval, if you need a different step, you just modify the interval and then call to_list/1 on it.

I think I would rename combine/1 to join/2, and make it work like Path.join/2 (perhaps with a corresponding Interval.join/1 which works like Path.join/1).

If you have these completed, feel free to open a PR, and we can either take it from there, or feel free to take the above adjustments into account - either way is fine :)

/cc @ckhrysze

mourjo commented 3 years ago

Is anyone working on Interval.join/2 and Interval.join/1? If not, I can create a pull request for it, mostly inspired from Clojure's clj-time. I am new to Elixir, but would definitely like to take a shot at this!

bitwalker commented 3 years ago

@mourjo I’m not aware of any such efforts at the moment, your contributions would be welcome!