Skip to content

Commit afa978a

Browse files
committed
Find the right time zone for standard times or daylight saving times
Using `.now` causes a search for "MST" in the winter, even if the time of the given schedule is in summer and would only match "MDT". By parsing the time value and using the components as values in the candidate time zone, this issue is fixed.
1 parent 3a4ab88 commit afa978a

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/ice_cube/parsers/ical_parser.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ def self.schedule_from_ical(ical_string, options = {})
55
ical_string.each_line do |line|
66
(property, value) = line.split(":")
77
(property, tzid) = property.split(";")
8-
zone = find_zone(tzid) if tzid.present?
8+
zone = find_zone(tzid, value) if tzid.present?
99
case property
1010
when "DTSTART"
1111
value = {time: value, zone: zone} if zone.present?
@@ -93,13 +93,15 @@ def self.rule_from_ical(ical)
9393
Rule.from_hash(params)
9494
end
9595

96-
private_class_method def self.find_zone(tzid)
96+
private_class_method def self.find_zone(tzid, time_string)
9797
(_, zone) = tzid&.split("=")
9898
begin
9999
Time.find_zone!(zone) if zone.present?
100100
rescue ArgumentError
101101
(rails_zone, _tzinfo_id) = ActiveSupport::TimeZone::MAPPING.find do |(k, _)|
102-
Time.find_zone!(k).now.strftime("%Z") == zone
102+
time = Time.parse(time_string)
103+
104+
Time.find_zone!(k).local(time.year, time.month, time.day, time.hour, time.min).strftime("%Z") == zone
103105
end
104106

105107
Time.find_zone(rails_zone)

spec/examples/from_ical_spec.rb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def sorted_ical(ical)
378378
end
379379
end
380380

381-
it "round trips from and to ical with time zones" do
381+
it "round trips from and to ical with time zones in the summer (MDT)" do
382382
original = <<-ICAL.gsub(/^\s*/, "").strip
383383
DTSTART;TZID=MDT:20130731T143000
384384
RRULE:FREQ=WEEKLY;UNTIL=20140730T203000Z;BYDAY=MO,WE,FR
@@ -393,6 +393,22 @@ def sorted_ical(ical)
393393
schedule_from_ical = IceCube::Schedule.from_ical original
394394
expect(schedule_from_ical.to_ical).to eq original
395395
end
396+
397+
it "round trips from and to ical with time zones in the winter (MST)" do
398+
original = <<-ICAL.gsub(/^\s*/, "").strip
399+
DTSTART;TZID=MST:20130131T143000
400+
RRULE:FREQ=WEEKLY;UNTIL=20140130T203000Z;BYDAY=MO,WE,FR
401+
RDATE;TZID=MST:20150212T143000
402+
RDATE;TZID=MST:20150207T143000
403+
EXDATE;TZID=MST:20130223T143000
404+
EXDATE;TZID=MST:20130212T143000
405+
EXDATE;TZID=MST:20130207T143000
406+
DTEND;TZID=MST:20130131T153000
407+
ICAL
408+
409+
schedule_from_ical = IceCube::Schedule.from_ical original
410+
expect(schedule_from_ical.to_ical).to eq original
411+
end
396412
end
397413

398414
describe "exceptions" do

0 commit comments

Comments
 (0)