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 def tzid
country = TZInfo::Country.get region.try(:code) || code country = TZInfo::Country.get region.try(:code) || code
if country.present? && country.zone_identifiers.length.positive? if country.present? && country.zone_identifiers.length.positive?
# Get arbitrarily the first zone for this country
country.zone_identifiers[0] country.zone_identifiers[0]
else else
Time.now.zone Time.now.zone

View File

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

View File

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