Attempt to not use a timezone when it is not recognized

This commit is contained in:
echarp 2019-04-27 17:10:45 +02:00
parent 4fb494d393
commit 59031bde86
3 changed files with 13 additions and 4 deletions

View File

@ -23,6 +23,7 @@ class Region < ApplicationRecord
def tzid
country = TZInfo::Country.get region.try(:code) || code
if country.present? && country.zone_identifiers.length.positive?
# Get arbitrarily the first zone for this country
country.zone_identifiers[0]
else
Time.now.zone

View File

@ -5,6 +5,7 @@
cal = Icalendar::Calendar.new
@events.each do |event|
tzid = event.region.tzid
use_timezone = true
if cal.timezones.none? { |t| tzid == t.tzid }
# Only add this zone once
begin
@ -13,14 +14,21 @@
cal.add_timezone timezone
rescue TZInfo::InvalidTimezoneIdentifier
# No need to add this tz
use_timezone = false
end
end
cal.event do |e|
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time, tzid: tzid
if use_timezone
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time, tzid: tzid
e.dtstart = Icalendar::Values::DateTime.new event.start_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new event.end_time, tzid: tzid
else
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time.localtime
e.dtstart = Icalendar::Values::DateTime.new event.start_time.localtime
e.dtend = Icalendar::Values::DateTime.new event.end_time.localtime
end
e.uid = "#{event.id}@#{request.domain}"
e.dtstart = Icalendar::Values::DateTime.new event.start_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new event.end_time, tzid: tzid
e.summary = event.title
e.description = to_markdown event.description.tr '\'', ''
e.location = event.full_address.tr '\'', ''

View File

@ -12,6 +12,6 @@ class RegionTest < ActiveSupport::TestCase
end
test 'check other timezone is local timezone' do
assert regions(:region_other).tzid == Time.now.zone
assert regions(:region_other).tzid == 'CEST'
end
end