Skip to content

Commit b4cd303

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 14da4a5 commit b4cd303

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

lib/ice_cube/parsers/ical_parser.rb

+5-3
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?
@@ -95,13 +95,15 @@ def self.rule_from_ical(ical)
9595

9696
private
9797

98-
def self.find_zone(tzid)
98+
def self.find_zone(tzid, time_string)
9999
(_, zone) = tzid&.split("=")
100100
begin
101101
Time.find_zone!(zone) if zone.present?
102102
rescue ArgumentError
103103
(rails_zone, _tzinfo_id) = ActiveSupport::TimeZone::MAPPING.find do |(k, _)|
104-
Time.find_zone!(k).now.strftime("%Z") == zone
104+
time = Time.parse(time_string)
105+
106+
Time.find_zone!(k).local(time.year, time.month, time.day, time.hour, time.min).strftime("%Z") == zone
105107
end
106108

107109
Time.find_zone(rails_zone)

spec/examples/from_ical_spec.rb

+17-1
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)