There must be something simple being overlooked here...
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.
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?
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.
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
Adding a recurrence rule
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>]
Then checking Date.today
works...
1.8.7 :025 > schedule.occurs_on? Date.today
=> true
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
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...?
Does IceCube
not work for single occurrence events with a start and end time?
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).
Then when people are searching rooms, the prices would be adjusted if the requested stay occurs_on?
a holiday price schedule
Do I need to store the start_time
and end_time
outside of the schedule and check it manually or something?
Or is there a better suited gem / tool to assist with this kind of schedule management?
You're misunderstanding how schedules and rules work.
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.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.
Secondly,
end_time
works together withstart_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.This creates a distinction between
occurs_at?(t1)
andoccurring_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.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:
After some more testing I think using IceCube's SingleOccurrenceRule is the proper way to have a single occurrence of an event.
To have a schedule that occurs only on the days between the Schedule
start_time
andend_time
I can do something like the following.Create an
IceCube::Schedule
with a start and end_time:Put all the days that occur within the schedule into an array.
Iterate over the array and create a SingleOccurrenceRule for each day in the schedule. Then test a couple dates. Within 30 days, occurs_on? is true, outside of 30 days, occurs_on? is false. This seems correct, except it still returns false when checking if
schedule.occurs_on? Date.today
. WHY?!?!?