Attempt to manage ical time zones using proper time zone TZID.

Refs #76 and refs #136
This commit is contained in:
echarp 2019-04-27 14:44:29 +02:00
parent 4307d2b7e2
commit 999f947aa4
6 changed files with 54 additions and 11 deletions

View File

@ -18,4 +18,17 @@ class Region < ApplicationRecord
def to_s def to_s
name name
end end
# Attempt to get a corresponding timezone, used for ical
def tzid
country = TZInfo::Country.get region.try(:code) || code
if country.present? && country.zone_identifiers.length.positive?
country.zone_identifiers[0]
else
Time.now.zone
end
rescue TZInfo::InvalidCountryCode
# Mostly useful for non country calendars
Time.now.zone
end
end end

View File

@ -2,14 +2,17 @@
# Create a calendar with an event (standard method) # Create a calendar with an event (standard method)
cal = Icalendar::Calendar.new cal = Icalendar::Calendar.new
@events.each do |event| @events.each do |event|
tzid = event.region.tzid
cal.event do |e| cal.event do |e|
e.dtstamp = Icalendar::Values::DateTime.new event.decision_time e.dtstamp = Icalendar::Values::DateTime.new event.decision_time, tzid: tzid
e.uid = "#{event.id}@#{request.domain}" e.uid = "#{event.id}@#{request.domain}"
e.dtstart = Icalendar::Values::DateTime.new event.start_time e.dtstart = Icalendar::Values::DateTime.new event.start_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new event.end_time e.dtend = Icalendar::Values::DateTime.new event.end_time, tzid: tzid
e.summary = event.title e.summary = event.title
e.description = event.description.tr '\'', '' e.description = to_markdown event.description.tr '\'', ''
e.location = event.full_address.tr '\'', '' e.location = event.full_address.tr '\'', ''
e.organizer = "mailto:#{event.contact}"
e.x_alt_desc = Icalendar::Values::Text.new event.description, FMTTYPE: 'text/html'
end end
end end

View File

@ -1,12 +1,17 @@
:ruby :ruby
# Create a calendar with an event (standard method) # Create a calendar with an event (standard method)
cal = Icalendar::Calendar.new cal = Icalendar::Calendar.new
tzid = @event.region.tzid
cal.event do |e| cal.event do |e|
e.dtstart = Icalendar::Values::DateTime.new @event.start_time e.dtstamp = Icalendar::Values::DateTime.new @event.decision_time, tzid: tzid
e.dtend = Icalendar::Values::DateTime.new @event.end_time 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.summary = @event.title
e.description = strip_tags(@event.description).tr '\'', '' e.description = to_markdown @event.description.tr '\'', ''
e.location = @event.full_address e.location = @event.full_address
e.organizer = "mailto:#{@event.contact}"
e.x_alt_desc = Icalendar::Values::Text.new @event.description, FMTTYPE: 'text/html'
end end
cal.publish cal.publish

View File

@ -1,7 +1,20 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
region_france:
name: France
code: FR
region_quebec:
name: Quebec
code: QC
region_one: region_one:
name: Alsace name: Alsace
region: region_france
region_two: region_two:
name: Aquitaine name: Aquitaine
region: region_france
region_other:
name: Other

View File

@ -118,6 +118,6 @@ class EventTest < ActiveSupport::TestCase
@event.city = 'world' @event.city = 'world'
@event.region.name = 'here' @event.region.name = 'here'
@event.region.code = 'xyz' @event.region.code = 'xyz'
assert_equal 'hello, world', @event.full_address assert_equal 'hello, world, here, France', @event.full_address
end end
end end

View File

@ -2,7 +2,16 @@ require 'test_helper'
# Mostly a data holder model, to maintain coutry > region data # Mostly a data holder model, to maintain coutry > region data
class RegionTest < ActiveSupport::TestCase class RegionTest < ActiveSupport::TestCase
# test "the truth" do test 'proper timezone' do
# assert true assert regions(:region_one).tzid.present?
# end assert regions(:region_one).tzid == 'Europe/Paris'
end
test 'check québec timezone is local timezone' do
assert regions(:region_quebec).tzid == Time.now.zone
end
test 'check other timezone is local timezone' do
assert regions(:region_other).tzid == Time.now.zone
end
end end