From f72cf4d3283e94ecafc813f14673ed66211b03d3 Mon Sep 17 00:00:00 2001 From: Eric-Paul Lecluse Date: Thu, 28 Apr 2022 12:00:20 +0200 Subject: [PATCH] Alternative time zone lookup using strftime, to complement Rails mapping Co-authored-by: Jankees van Woezik --- lib/ice_cube/parsers/ical_parser.rb | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/ice_cube/parsers/ical_parser.rb b/lib/ice_cube/parsers/ical_parser.rb index 1de093ad..22800376 100644 --- a/lib/ice_cube/parsers/ical_parser.rb +++ b/lib/ice_cube/parsers/ical_parser.rb @@ -5,7 +5,7 @@ def self.schedule_from_ical(ical_string, options = {}) ical_string.each_line do |line| (property, value) = line.split(":") (property, tzid) = property.split(";") - zone = Time.find_zone(tzid) if tzid.present? + zone = find_zone(tzid) if tzid.present? case property when "DTSTART" value = { time: value, zone: zone } if zone.present? @@ -92,5 +92,21 @@ def self.rule_from_ical(ical) Rule.from_hash(params) end + + private + + def self.find_zone(tzid) + (_, zone) = tzid&.split("=") + begin + Time.find_zone!(zone) if zone.present? + rescue ArgumentError + (rails_zone, tzinfo_id) = ActiveSupport::TimeZone::MAPPING.find do |(k, _)| + candidate = Time.find_zone!(k) + candidate.now.strftime("%Z") == zone + end + + Time.find_zone(rails_zone) + end + end end end