Module: FoodsoftDateUtil

Defined in:
lib/foodsoft_date_util.rb

Class Method Summary collapse

Class Method Details

.next_occurrence(start = Time.now, from = start, options = {}) ⇒ Object

find next occurence given a recurring ical string and time


3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/foodsoft_date_util.rb', line 3

def self.next_occurrence(start=Time.now, from=start, options={})
  occ = nil
  if options && options[:recurr]
    schedule = IceCube::Schedule.new(start)
    schedule.add_recurrence_rule rule_from(options[:recurr])
    # @todo handle ical parse errors
    occ = (schedule.next_occurrence(from).to_time rescue nil)
  end
  if options && options[:time] && occ
    occ = occ.beginning_of_day.advance(seconds: Time.parse(options[:time]).seconds_since_midnight)
  end
  occ
end

.rule_from(p) ⇒ IceCube::Rule


19
20
21
22
23
24
25
26
27
# File 'lib/foodsoft_date_util.rb', line 19

def self.rule_from(p)
  if p.is_a? String
    IceCube::Rule.from_ical(p)
  elsif p.is_a? Hash
    IceCube::Rule.from_hash(p)
  else
    p
  end
end