foodsoft/app/lib/foodsoft_date_util.rb

32 lines
935 B
Ruby
Raw Permalink Normal View History

module FoodsoftDateUtil
# find next occurence given a recurring ical string and time
def self.next_occurrence(start = Time.now, from = start, options = {})
2015-10-23 14:53:01 +02:00
occ = nil
if options && options[:recurr]
schedule = IceCube::Schedule.new(start)
schedule.add_recurrence_rule rule_from(options[:recurr])
# @todo handle ical parse errors
2023-01-06 16:27:41 +01:00
occ = begin
schedule.next_occurrence(from).to_time
rescue StandardError
2023-01-06 16:27:41 +01:00
nil
end
end
occ = occ.beginning_of_day.advance(seconds: Time.parse(options[:time]).seconds_since_midnight) if options && options[:time] && occ
occ
end
# @param rule [String, Symbol, Hash, IceCube::Rule] What to return a rule from.
# @return [IceCube::Rule] Recurring rule
def self.rule_from(rule)
case rule
2023-01-06 16:27:41 +01:00
when String
IceCube::Rule.from_ical(rule)
2023-01-06 16:27:41 +01:00
when Hash
IceCube::Rule.from_hash(rule)
else
rule
end
end
end