Attempt to not use a timezone when it is not recognized

remotes/origin/master
echarp 3 years ago
parent 4fb494d393
commit 59031bde86
  1. 1
      app/models/region.rb
  2. 14
      app/views/events/index.ics.haml
  3. 2
      test/models/region_test.rb

@ -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

@ -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 '\'', '’'

@ -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

Loading…
Cancel
Save