2014-11-22 00:33:16 +01:00
|
|
|
module FoodsoftDateUtil
|
|
|
|
# find next occurence given a recurring ical string and time
|
2021-03-01 15:27:26 +01:00
|
|
|
def self.next_occurrence(start = Time.now, from = start, options = {})
|
2015-10-23 14:53:01 +02:00
|
|
|
occ = nil
|
|
|
|
if options && options[:recurr]
|
2014-11-22 00:33:16 +01:00
|
|
|
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
|
2023-05-12 13:01:12 +02:00
|
|
|
rescue StandardError
|
2023-01-06 16:27:41 +01:00
|
|
|
nil
|
|
|
|
end
|
2014-11-22 00:33:16 +01:00
|
|
|
end
|
2023-05-12 13:01:12 +02:00
|
|
|
occ = occ.beginning_of_day.advance(seconds: Time.parse(options[:time]).seconds_since_midnight) if options && options[:time] && occ
|
2014-11-22 00:33:16 +01:00
|
|
|
occ
|
|
|
|
end
|
|
|
|
|
2023-05-12 13:01:12 +02:00
|
|
|
# @param rule [String, Symbol, Hash, IceCube::Rule] What to return a rule from.
|
2014-11-22 00:33:16 +01:00
|
|
|
# @return [IceCube::Rule] Recurring rule
|
2023-05-12 13:01:12 +02:00
|
|
|
def self.rule_from(rule)
|
|
|
|
case rule
|
2023-01-06 16:27:41 +01:00
|
|
|
when String
|
2023-05-12 13:01:12 +02:00
|
|
|
IceCube::Rule.from_ical(rule)
|
2023-01-06 16:27:41 +01:00
|
|
|
when Hash
|
2023-05-12 13:01:12 +02:00
|
|
|
IceCube::Rule.from_hash(rule)
|
2014-11-22 00:33:16 +01:00
|
|
|
else
|
2023-05-12 13:01:12 +02:00
|
|
|
rule
|
2014-11-22 00:33:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|