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

Interval equality #544

Closed
svarlet opened this issue Jul 8, 2019 · 5 comments
Closed

Interval equality #544

svarlet opened this issue Jul 8, 2019 · 5 comments
Labels

Comments

@svarlet
Copy link

svarlet commented Jul 8, 2019

Hi,

First of all, thanks for the great work, this is such a good library and so useful!

I am using Timex.Interval and I am suddenly wondering how to test equality of 2 intervals? There isn't any function for this. I'm tempted to use one of the others like contains?/2 and do this:

def equal?(A, B) do
  contains?(A, B) && contains?(B, A) 
end

Would it work? is there a better way?

As someone noted in the slack chat, the steps property would certainly make my implementation of equal wrong. In this particular instance I don't use the steps property so I'd be fine.

@bitwalker
Copy link
Owner

To test exact equality, a == b will suffice, as equality of the structs reflects equality of the intervals they represent (this works since intervals are normalized to NaiveDateTime/UTC). Two intervals with the same range, but different steps are not equal, so as you point out, it would be incorrect to return true for something like equal?. Other than exact equality, typically you only care about overlap/containment, or whether an interval comes before/after another interval.

Do you have a use case that calls for needing to know whether an interval range is precisely the same, but where the step of the interval doesn't matter?

@svarlet
Copy link
Author

svarlet commented Jul 9, 2019

Hi @bitwalker ,

Thanks for the quick response. On a side project I decided to use Interval(s) to represent the date at which a product becomes available and the date at which it expires. That could be when your bag of apples is purchasable online and when they disappear from the website because they are completely rotten at this date. It could be when an early bird ticket to Elixir Conf becomes available and when it's not anymore. In both examples, I don't need the steps. Perhaps I underestimate their power?

@NobbZ
Copy link

NobbZ commented Jul 9, 2019

Don't you think that Date.range/2 might be a better choice then?

@bitwalker
Copy link
Owner

@svarlet as mentioned by @NobbZ, the use of Date.range or a pair of DateTime/NaiveDateTime and Timex.between? is a better match for your use case. Intervals are intended to represent recurring events, such as a meeting that occurs every tuesday at 3pm; or cron job that runs every hour. For something that is simply on/off, a range works best.

@svarlet
Copy link
Author

svarlet commented Jul 9, 2019

Thanks for the feedback. I didn't know about Date.range but unfortunately that won't quite work for me. Date.range only works with Date, not DateTime.
Lacking this, Interval still is a good solution for me, especially because it combines all the data I need in one thing and offers a good API to manipulate this data structure (both available functions and how fluent their arguments feel: Interval.new(from: date, until: duration) is excellent.

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

No branches or pull requests

3 participants