使用start_time和end_time使用ice_cube gem发生单次事件

这里必须有一些简单的东西被忽视……

我一直在尝试各种方法来创建基本的IceCube计划( https://github.com/seejohnrun/ice_cube )。 总体目标是使用IceCube在“房间预订”rails应用程序中允许“价格表”。

第一种情况是创建具有特定start_timeend_time的基本schedule – 仅发生一次。 IceCube可以做到这一点,对吗?

计划将在start_time开始,并在end_time结束。 我希望能够检查日期或时间是否发生occurs_on? 此计划确定是否应调整房价。

所以在控制台中我尝试创建一个基本的时间表,并且预计它将从现在开始发生5.days ,因为start_timeTime.nowend_timeTime.now + 30.days 。 但它似乎永远不会真实……

 1.8.7 :001 > schedule = IceCube::Schedule.new(Time.now, :end_time => Time.now + 30.days) => # 1.8.7 :002 > schedule.occurs_on? Date.today + 5.days => false 1.8.7 :005 > schedule.occurs_at? Time.now + 5.days => false 1.8.7 :006 > schedule.occurring_at? Time.now + 5.days => false 

添加重复规则

 1.8.7 :018 > schedule.rrule IceCube::Rule.monthly => [#[#], :base_hour=>[#], :interval=>[#], :base_min=>[#], :base_sec=>[#]}, @interval=1>] 

然后检查Date.today工作…

 1.8.7 :025 > schedule.occurs_on? Date.today => true 

但是检查发生了什么? 对于Date.today + 10.days仍然返回false …为什么?

 1.8.7 :026 > schedule.occurs_on? Date.today + 10.days => false 

那我在忽视/做错了什么? 或者设置IceCube::Schedule start_timeend_time有什么end_time – 它们似乎没有效果……?

IceCube不适用于具有开始和结束时间的单次事件吗?

另一个示例场景,房间所有者希望在假日季节提高房价。 因此,房间所有者创建了一个价格表,从2012年12月1日开始到2013年1月7日结束。(不应该重复,但如果所有者需要,可以)。

然后当人们搜索房间时,如果请求的住宿发生在occurs_on? ,价格会被调整occurs_on? 节日价格表

我是否需要将start_timeend_time存储在end_time之外并手动检查或者其他内容?

或者是否有更适合的gem/工具来协助这种日程管理?

您误解了计划和规则的工作原理。

首先,了解start_time很重要。 计划的每次出现都基于此,并且计划返回与开始时间的特定间隔匹配的时间。 间隔由规则确定。

您的示例不起作用,因为“从现在起5天”不是从计划的开始时间开始的每月间隔。 从开始时间开始的28,30或31天将匹配,具体取决于月份。

 start = Time.utc(2013, 05, 17, 12, 30, 00) # 2013-05-17 12:30:00 UTC schedule = IceCube::Schedule.new(start) schedule.add_recurrence_rule IceCube::Rule.monthly schedule.occurs_on? start + 5.days #=> false schedule.occurs_on? start + 31.days #=> true 

其次, end_timestart_time一起使用以设置每次出现的持续时间。 因此,如果您的开始时间是09:00,结束时间是17:00,则每次出现的持续时间为8小时。

这会在occurs_at?(t1)occurs_at?(t1)之间产生区别:第一个仅在给定时间与事件的开始完全匹配时才为真; 第二个在持续时间的任何时间都是真的。 occurs_on?(d1)匹配给定日期中的任何时间。

 arrival = Time.utc(2013, 5, 1, 9, 0, 0) departure = Time.utc(2013, 5, 1, 17, 0, 0) schedule = IceCube::Schedule.new(arrival, end_time: departure) schedule.add_recurrence_rule IceCube::Rule.weekly.day(1, 2, 3, 4, 5) # MF schedule.occurs_at? arrival #=> true schedule.occurs_at? arrival + 1.second #=> false schedule.occurring_at? arrival + 1.second #=> true schedule.occurring_at? departure + 1.second #=> false 

对于您正在做的事情,您可以尝试以下两种方法之一:

  1. 一个月的发生
  2. 每月发生一个月后结束

这取决于您需要如何根据计划显示或validation时间。 以下是两个例子:

 arrival = Time.utc(2013, 5, 1) departure = Time.utc(2013, 5, 31) # single occurrence schedule = IceCube::Schedule.new(arrival, duration: 31.days) # daily occurrence schedule = IceCube::Schedule.new(arrival, duration: 1.day) schedule.add_recurrence_rule IceCube::Rule.daily.until(departure) 

经过一些测试后,我认为使用IceCube的SingleOccurrenceRule是一次事件发生的正确方法。

要使计划仅在Schedule start_timeend_time之间的日期发生,我可以执行以下操作。

使用start和end_time创建IceCube::Schedule

 1.8.7 :097 > schedule = IceCube::Schedule.new(Time.now, :end_time => Time.now + 30.days) => # 

将计划中发生的所有日期放入数组中。

 1.8.7 :098 > days_in_schedule = [] => [] 1.8.7 :099 > schedule.start_time.to_date.upto(schedule.end_time.to_date) { |d| puts d; days_in_schedule << d } 

迭代数组并为计划中的每一天创建SingleOccurrenceRule。 然后测试几个日期。 在30天内,发生在? 是的,在30天之外,发生了吗? 是假的。 这似乎是正确的,除了在检查schedule.occurs_on? Date.today时它仍然返回false schedule.occurs_on? Date.today schedule.occurs_on? Date.today为什么?!?!?

 1.8.7 :100 > days_in_schedule.each { |d| schedule.rtime Time.parse(d.to_s) } 1.8.7 :109 > schedule.terminating? => true 1.8.7 :110 > schedule.occurs_on? Date.today + 5.days => true 1.8.7 :111 > schedule.occurs_on? Date.today + 55.days => false 1.8.7 :135 > schedule.occurs_on? Date.today => false