foodsoft/app/lib/foodsoft_date_util.rb
Philipp Rothmann fb2b4d8a8a chore: rubocop
chore: fix api test conventions

chore: rubocop -A spec/

chore: more rubocop -A

fix failing test

rubocop fixes

removes helper methods that are in my opinion dead code

more rubocop fixes

rubocop -a --auto-gen-config
2023-06-09 17:35:05 +02:00

31 lines
935 B
Ruby

module FoodsoftDateUtil
# find next occurence given a recurring ical string and time
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 = begin
schedule.next_occurrence(from).to_time
rescue StandardError
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
when String
IceCube::Rule.from_ical(rule)
when Hash
IceCube::Rule.from_hash(rule)
else
rule
end
end
end