Once a schedule is defined, you can query it to find specific occurrences or check if a time falls within the schedule.
Common Query Methods
occurrences(end_time): List occurrences until end_time (required for non-terminating rules).all_occurrences: Returns all occurrences (only for terminating schedules).occurs_at?(time): Checks if a specific time is part of the schedule.occurs_on?(date): Checks if a specific day is part of the schedule.occurs_between?(start, end): Checks if the schedule occurs within a date range.first(n): Returns the first n occurrences (or just the first if n is omitted).last(n): Returns the last n occurrences (if the schedule terminates).next_occurrence(from_time): Returns the next occurrence after from_time (defaults to Time.now).previous_occurrence(from_time): Returns the previous occurrence before from_time.each_occurrence { |t| ... }: Iterates through occurrences.
Handling Durations and Spans
If you provide a duration to the Schedule, you can use occurring_at? and occurring_between? to check if the schedule's interval overlaps with a given time.
To include prior occurrences that overlap a specific time, use the spans: true option:
next_occurrences(n, from_time, spans: true)occurrences_between(from_time, to_time, spans: true)
require 'ice_cube'
require 'active_support/time'
schedule = IceCube::Schedule.new(now = Time.now) do |s|
s.add_recurrence_rule(IceCube::Rule.daily.count(4))
s.add_exception_time(now + 1.day)
end
# Examples
schedule.occurrences(now + 10.days)
schedule.occurs_at?(now + 2.days)
schedule.occurs_between?(now, now + 30.days)
schedule.first(2)
schedule.next_occurrence(now)