如何测试两个时间范围是否重叠? [英] How to test whether two time ranges overlap?

查看:160
本文介绍了如何测试两个时间范围是否重叠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现预订功能,并确保预订不会在Rails应用程序中重叠。

I need to implement booking functionality and ensure that bookings don't overlap in a Rails app.

封面?之间?方法不是我所需要的。与同一模型上的其他潜在范围相比,我必须确保时间范围的唯一性,并有效地做到这一点。

The cover? and between? methods aren't quite what I need. I have to ensure uniqueness of a time range when compared to other potential ranges on the same model, and do it efficiently.

我认为可以使用 重叠? 。问题在于,对于以下内容,它返回TRUE:

I THINK it can be done using overlaps?. The problem is that this returns TRUE for something like this:

(1..5).overlaps?(5..9)
=> true

如果我比较的是刚好在另一个开始时就结束的预订( 3: 30-4:00 4:00-4:30 ),则表示它们确实重叠,但从技术上讲它们并不重叠。

If I compared a booking that ended right when another started (3:30 - 4:00 versus 4:00 - 4:30), it would say they do overlap, but they technically don't. That would be a problem right?

ValidatesOverlap 似乎可以解决这个问题,包括边缘重叠。

ValidatesOverlap seems to handle the issue, including edge overlap.

有什么建议吗?

推荐答案

def overlap?(x,y)
  (x.first - y.end) * (y.first - x.end) > 0
end

将起作用。问题是 Interval 使用> = ,如您在 测试两个日期范围在Ruby还是Rails中是否重叠

will work. The problem is that Interval uses >=, as you can see in "Test if two date ranges overlap in Ruby or Rails".

这篇关于如何测试两个时间范围是否重叠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆