使用 start_time 和 end_time 的 ice_cube gem 单次发生事件 [英] Single occurrence event with ice_cube gem using start_time and end_time
问题描述
这里肯定有一些简单的东西被忽略了......
There must be something simple being overlooked here...
我一直在尝试各种方法来创建基本的 IceCube
计划(https://github.com/seejohnrun/ice_cube).总体目标是使用 IceCube
在房间预订"rails 应用程序中允许价格表".
I've been trying various methods for creating a basic IceCube
schedule (https://github.com/seejohnrun/ice_cube). The overall goal is to use IceCube
to allow "price schedules" inside a "room reservation" rails application.
第一个场景是创建一个基本的schedule
,带有特定的start_time
和end_time
- 只发生一次.IceCube
可以做到这一点,对吗?
The first scenario would be creating a basic schedule
with a specific start_time
and end_time
- occurring only once. IceCube
can do this, correct?
计划将从 start_time
开始并在 end_time
结束.我希望能够检查日期或时间是否occurs_on?
这个时间表来确定是否应该调整房间价格.
The schedule would begin on the start_time
and end at the end_time
. I would expect to be able to check if dates or times occurs_on?
this schedule to determine if a room price should be adjusted.
所以在控制台中,我尝试创建一个基本时间表,并希望它从现在开始 5.days
发生,因为 start_time
是 Time.now
和 end_time
是 Time.now + 30.days
.但它似乎永远不会返回 true...
So in console I've tried creating a basic schedule and would expect it to be occurring 5.days
from now since the start_time
is Time.now
and the end_time
is Time.now + 30.days
. But it seems to never return true...
1.8.7 :001 > schedule = IceCube::Schedule.new(Time.now, :end_time => Time.now + 30.days)
=> #<IceCube::Schedule:0xb619d604 @all_recurrence_rules=[], @duration=nil, @end_time=Tue Jan 08 09:13:11 -0600 2013, @all_exception_rules=[], @start_time=Sun Dec 09 09:13:11 -0600 2012>
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
=> [#<IceCube::MonthlyRule:0xb687a88c @validations={:base_day=>[#<IceCube::Validations::ScheduleLock::Validation:0xb6875b0c @type=:day>], :base_hour=>[#<IceCube::Validations::ScheduleLock::Validation:0xb6875abc @type=:hour>], :interval=>[#<IceCube::Validations::MonthlyInterval::Validation:0xb6875d28 @interval=1>], :base_min=>[#<IceCube::Validations::ScheduleLock::Validation:0xb6875a6c @type=:min>], :base_sec=>[#<IceCube::Validations::ScheduleLock::Validation:0xb6875a1c @type=:sec>]}, @interval=1>]
然后检查 Date.today
工作...
Then checking Date.today
works...
1.8.7 :025 > schedule.occurs_on? Date.today
=> true
但是检查发生了_on?对于 Date.today + 10.days
仍然返回 false ... 为什么?
But checking occurs_on? for Date.today + 10.days
still returns false... Why?
1.8.7 :026 > schedule.occurs_on? Date.today + 10.days
=> false
那么我忽略/做错了什么?或者设置 IceCube::Schedule start_time
和 end_time
有什么意义 - 它们似乎没有效果......?
So what am I overlooking / doing wrong? Or what is the point of setting an IceCube::Schedule start_time
and end_time
- they seem to have no effect...?
IceCube
是否不适用于具有开始和结束时间的单次事件?
Does IceCube
not work for single occurrence events with a start and end time?
另一个示例场景,客房所有者希望在假期期间提高房价.因此,房间所有者创建了一个从 2012 年 12 月 1 日开始到 2013 年 1 月 7 日结束的价格表.(不应该必须重复发生,但如果所有者愿意,可以重复发生).
Another example scenario, a room owner wants room prices raised for a holiday season. So the room owner creates a price schedule that starts on Dec 1 2012 and ends Jan 7 2013. (shouldn't have to recur, but could if the owner wanted).
那么当人们搜索房间时,如果请求的住宿occurs_on?
假期价格表
Then when people are searching rooms, the prices would be adjusted if the requested stay occurs_on?
a holiday price schedule
我是否需要在计划之外存储 start_time
和 end_time
并手动检查它或其他什么?
Do I need to store the start_time
and end_time
outside of the schedule and check it manually or something?
或者是否有更适合的 gem/工具来协助这种日程管理?
Or is there a better suited gem / tool to assist with this kind of schedule management?
推荐答案
您误解了时间表和规则的工作原理.
You're misunderstanding how schedules and rules work.
首先,了解start_time
很重要.计划的每次出现都基于此,计划返回与开始时间的特定时间间隔相匹配的时间.间隔由规则确定.
Firstly, it's important to understand start_time
. Every occurrence of the schedule is based on this, and the schedule returns times that match specific intervals from the start time. The intervals are determined by Rules.
您的示例不起作用,因为从现在起 5 天"不是计划开始时间的每月间隔.从开始时间起 28、30 或 31 天将匹配,具体取决于月份.
Your example doesn't work because "5 days from now" is not a monthly interval from the schedule's start time. 28, 30, or 31 days from the start time would match, depending on the month.
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_time
与 start_time
一起设置每次出现的持续时间.因此,如果您的开始时间是 09:00,结束时间是 17:00,那么每次发生的持续时间为 8 小时.
Secondly, end_time
works together with start_time
to set the duration of each occurrence. So if your start time is 09:00, and end time is 17:00 then each occurrence will have a duration of 8 hours.
这在 occurs_at?(t1)
和 occurring_at?(t1)
之间产生了区别:第一个只有当给定的时间完全匹配开始时才为真发生;第二个在持续时间内的任何时间都为真.occurs_on?(d1)
匹配给定日期中的任何时间.
This creates a distinction between occurs_at?(t1)
and occurring_at?(t1)
: the first one is only true when the given time exactly matches the start of an occurrence; the second one is true for any time in the duration. occurs_on?(d1)
matches for any time in the given date.
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) # M-F
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
对于您正在做的事情,您可以尝试以下两种方法之一:
For what you're doing, you could try one of two approaches:
- 一个月的事件
- 一个月后结束的每日事件
这取决于您需要如何根据时间表显示或验证时间.以下是两者的示例:
This depends on how you need to display or validate times against the schedule. Here's an example of both:
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)
这篇关于使用 start_time 和 end_time 的 ice_cube gem 单次发生事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!