icalendar 2.13.0 → 2.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +37 -0
- data/icalendar.gemspec +1 -0
- data/lib/icalendar/calendar.rb +98 -0
- data/lib/icalendar/timezone.rb +61 -1
- data/lib/icalendar/timezone_history.rb +94 -0
- data/lib/icalendar/transition_rule_sample.rb +58 -0
- data/lib/icalendar/tzinfo.rb +65 -3
- data/lib/icalendar/version.rb +1 -1
- data/spec/timezone_merging_spec.rb +595 -0
- data/spec/transition_rule_sample_spec.rb +81 -0
- data/spec/tzinfo_future_recurrence_spec.rb +98 -0
- metadata +23 -1
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require_relative '../lib/icalendar/tzinfo'
|
|
3
|
+
|
|
4
|
+
# Adding the same timezone more than once (for example, once per event) should
|
|
5
|
+
# still produce a single VTIMEZONE per TZID when the calendar is serialized.
|
|
6
|
+
# When the timezone's rules differ across the added dates, that single
|
|
7
|
+
# VTIMEZONE should hold multiple DAYLIGHT/STANDARD observances covering them.
|
|
8
|
+
describe 'Generating a calendar with a timezone added multiple times' do
|
|
9
|
+
let(:calendar) { Icalendar::Calendar.new }
|
|
10
|
+
|
|
11
|
+
def add_timezone(identifier, date)
|
|
12
|
+
calendar.add_timezone TZInfo::Timezone.get(identifier).ical_timezone(date)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def output
|
|
16
|
+
calendar.to_ical
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def vtimezone_count
|
|
20
|
+
output.scan(/^BEGIN:VTIMEZONE\r$/).length
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def tzid_lines
|
|
24
|
+
output.scan(/^TZID:.*\r$/).map(&:chomp)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def parsed_timezones
|
|
28
|
+
Icalendar::Calendar.parse(output).first.timezones
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def parsed_timezone(identifier)
|
|
32
|
+
parsed_timezones.find { |tz| tz.tzid.to_s == identifier }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Counted across every VTIMEZONE with the TZID so that duplicated
|
|
36
|
+
# VTIMEZONEs are not hidden by only looking at the first one.
|
|
37
|
+
def daylights(identifier)
|
|
38
|
+
parsed_timezones.select { |tz| tz.tzid.to_s == identifier }.flat_map(&:daylights)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def standards(identifier)
|
|
42
|
+
parsed_timezones.select { |tz| tz.tzid.to_s == identifier }.flat_map(&:standards)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The merged VTIMEZONE must still describe the correct UTC offset at each of
|
|
46
|
+
# the local times it was generated for.
|
|
47
|
+
def expect_offsets_to_match_tzinfo(identifier, local_times)
|
|
48
|
+
tzinfo = TZInfo::Timezone.get identifier
|
|
49
|
+
timezone = parsed_timezone identifier
|
|
50
|
+
local_times.each do |local|
|
|
51
|
+
expected = tzinfo.period_for_local(local).offset.ical_offset
|
|
52
|
+
offset = timezone.offset_for_local(local)
|
|
53
|
+
# offset_for_local returns the String "+00:00" when no observance applies
|
|
54
|
+
actual = offset.respond_to?(:value_ical) ? offset.value_ical : offset.delete(':')
|
|
55
|
+
expect(actual).to eq(expected), "#{identifier} at #{local}: expected #{expected}, got #{actual}"
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
context 'when the same timezone is added twice for the same date' do
|
|
60
|
+
let(:date) { DateTime.new 2021, 1, 15 }
|
|
61
|
+
|
|
62
|
+
before do
|
|
63
|
+
add_timezone 'Europe/Brussels', date
|
|
64
|
+
add_timezone 'Europe/Brussels', date
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'outputs a single VTIMEZONE' do
|
|
68
|
+
expect(vtimezone_count).to eq 1
|
|
69
|
+
expect(tzid_lines).to eq ['TZID:Europe/Brussels']
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it 'outputs a single daylight and standard observance' do
|
|
73
|
+
expect(daylights('Europe/Brussels').length).to eq 1
|
|
74
|
+
expect(standards('Europe/Brussels').length).to eq 1
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
context 'when the same Timezone object is added twice' do
|
|
79
|
+
before do
|
|
80
|
+
timezone = TZInfo::Timezone.get('Europe/Brussels').ical_timezone(DateTime.new(2021, 1, 15))
|
|
81
|
+
calendar.add_timezone timezone
|
|
82
|
+
calendar.add_timezone timezone
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
it 'outputs a single VTIMEZONE' do
|
|
86
|
+
expect(vtimezone_count).to eq 1
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
context 'when the timezone is added for dates in different years with the same rules' do
|
|
91
|
+
# The reproduction from issue #205
|
|
92
|
+
let(:dates) { [DateTime.new(2019, 3, 1, 12), DateTime.new(2019, 3, 1, 12) + 356] }
|
|
93
|
+
|
|
94
|
+
before do
|
|
95
|
+
dates.each { |date| add_timezone 'Europe/Brussels', date }
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
it 'outputs a single VTIMEZONE' do
|
|
99
|
+
expect(vtimezone_count).to eq 1
|
|
100
|
+
expect(tzid_lines).to eq ['TZID:Europe/Brussels']
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
it 'does not duplicate observances that share the same recurrence rule' do
|
|
104
|
+
expect(daylights('Europe/Brussels').length).to eq 1
|
|
105
|
+
expect(standards('Europe/Brussels').length).to eq 1
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
it 'describes the correct offsets for both years' do
|
|
109
|
+
local_times = [2019, 2020].flat_map do |year|
|
|
110
|
+
[DateTime.new(year, 1, 15, 12), DateTime.new(year, 7, 15, 12), DateTime.new(year, 12, 15, 12)]
|
|
111
|
+
end
|
|
112
|
+
expect_offsets_to_match_tzinfo 'Europe/Brussels', local_times
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
context 'when the timezone is added for the start and end of many events' do
|
|
117
|
+
let(:starts) do
|
|
118
|
+
[DateTime.new(2025, 9, 4, 12, 51, 24), DateTime.new(2025, 11, 28, 12, 51, 24),
|
|
119
|
+
DateTime.new(2026, 2, 28, 12, 51, 24), DateTime.new(2026, 5, 28, 12, 51, 24),
|
|
120
|
+
DateTime.new(2026, 8, 28, 12, 51, 24)]
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
before do
|
|
124
|
+
starts.each do |start|
|
|
125
|
+
finish = start + Rational(2, 24)
|
|
126
|
+
add_timezone 'Europe/Berlin', start
|
|
127
|
+
add_timezone 'Europe/Berlin', finish
|
|
128
|
+
calendar.event do |e|
|
|
129
|
+
e.dtstart = Icalendar::Values::DateTime.new start, 'tzid' => 'Europe/Berlin'
|
|
130
|
+
e.dtend = Icalendar::Values::DateTime.new finish, 'tzid' => 'Europe/Berlin'
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it 'outputs a single VTIMEZONE' do
|
|
136
|
+
expect(vtimezone_count).to eq 1
|
|
137
|
+
expect(tzid_lines).to eq ['TZID:Europe/Berlin']
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
it 'still outputs every event' do
|
|
141
|
+
expect(output.scan(/^BEGIN:VEVENT\r$/).length).to eq starts.length
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it 'describes the correct offset for every event' do
|
|
145
|
+
expect_offsets_to_match_tzinfo 'Europe/Berlin', starts
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
context 'when the timezone rules change between the added dates' do
|
|
150
|
+
# The US moved its DST transitions in 2007:
|
|
151
|
+
# 2006: first Sunday in April -> last Sunday in October
|
|
152
|
+
# 2008: second Sunday in March -> first Sunday in November
|
|
153
|
+
let(:dates) { [DateTime.new(2006, 7, 1, 12), DateTime.new(2008, 7, 1, 12)] }
|
|
154
|
+
|
|
155
|
+
before do
|
|
156
|
+
dates.each { |date| add_timezone 'America/New_York', date }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it 'outputs a single VTIMEZONE' do
|
|
160
|
+
expect(vtimezone_count).to eq 1
|
|
161
|
+
expect(tzid_lines).to eq ['TZID:America/New_York']
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
it 'includes multiple daylight and standard observances' do
|
|
165
|
+
timezone = parsed_timezones.first
|
|
166
|
+
expect(timezone.daylights.length).to be >= 2
|
|
167
|
+
expect(timezone.standards.length).to be >= 2
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it 'describes the correct offsets under both sets of rules' do
|
|
171
|
+
local_times = [
|
|
172
|
+
DateTime.new(2007, 3, 20, 12), # daylight under new rules, standard under old
|
|
173
|
+
DateTime.new(2006, 7, 1, 12),
|
|
174
|
+
DateTime.new(2006, 11, 1, 12), # standard under old rules, daylight under new
|
|
175
|
+
DateTime.new(2008, 3, 20, 12),
|
|
176
|
+
DateTime.new(2008, 7, 1, 12),
|
|
177
|
+
DateTime.new(2008, 11, 1, 12)
|
|
178
|
+
]
|
|
179
|
+
expect_offsets_to_match_tzinfo 'America/New_York', local_times
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it 'does not depend on the order the timezones were added' do
|
|
183
|
+
reversed = Icalendar::Calendar.new
|
|
184
|
+
dates.reverse_each do |date|
|
|
185
|
+
reversed.add_timezone TZInfo::Timezone.get('America/New_York').ical_timezone(date)
|
|
186
|
+
end
|
|
187
|
+
expect(reversed.to_ical.scan(/^BEGIN:VTIMEZONE\r$/).length).to eq 1
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
context 'when daylight saving time is abolished between the added dates' do
|
|
192
|
+
# Brazil stopped observing DST in 2019
|
|
193
|
+
let(:dates) { [DateTime.new(2018, 12, 1, 12), DateTime.new(2020, 7, 1, 12)] }
|
|
194
|
+
|
|
195
|
+
before do
|
|
196
|
+
dates.each { |date| add_timezone 'America/Sao_Paulo', date }
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
it 'outputs a single VTIMEZONE' do
|
|
200
|
+
expect(vtimezone_count).to eq 1
|
|
201
|
+
expect(tzid_lines).to eq ['TZID:America/Sao_Paulo']
|
|
202
|
+
end
|
|
203
|
+
|
|
204
|
+
it 'describes the correct offsets before and after the change' do
|
|
205
|
+
local_times = [DateTime.new(2018, 12, 1, 12), DateTime.new(2020, 1, 15, 12), DateTime.new(2020, 7, 1, 12)]
|
|
206
|
+
expect_offsets_to_match_tzinfo 'America/Sao_Paulo', local_times
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
context 'when a timezone without daylight saving time is added twice' do
|
|
211
|
+
before do
|
|
212
|
+
add_timezone 'Asia/Tokyo', DateTime.new(2020, 1, 15)
|
|
213
|
+
add_timezone 'Asia/Tokyo', DateTime.new(2021, 7, 15)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
it 'outputs a single VTIMEZONE with a single standard observance' do
|
|
217
|
+
expect(vtimezone_count).to eq 1
|
|
218
|
+
expect(standards('Asia/Tokyo').length).to eq 1
|
|
219
|
+
expect(daylights('Asia/Tokyo')).to be_empty
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
context 'when multiple different timezones are each added multiple times' do
|
|
224
|
+
before do
|
|
225
|
+
add_timezone 'Europe/Brussels', DateTime.new(2021, 1, 15)
|
|
226
|
+
add_timezone 'America/Chicago', DateTime.new(2021, 1, 15)
|
|
227
|
+
add_timezone 'Europe/Brussels', DateTime.new(2022, 7, 15)
|
|
228
|
+
add_timezone 'America/Chicago', DateTime.new(2022, 7, 15)
|
|
229
|
+
add_timezone 'Europe/Brussels', DateTime.new(2021, 1, 15)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
it 'outputs one VTIMEZONE per TZID' do
|
|
233
|
+
expect(vtimezone_count).to eq 2
|
|
234
|
+
expect(tzid_lines).to contain_exactly 'TZID:Europe/Brussels', 'TZID:America/Chicago'
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
it 'keeps the timezones in the order they were first added' do
|
|
238
|
+
expect(tzid_lines).to eq ['TZID:Europe/Brussels', 'TZID:America/Chicago']
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|
|
242
|
+
|
|
243
|
+
describe 'TZInfo::Timezone#ical_timezone_for_range' do
|
|
244
|
+
def round_trip(timezone)
|
|
245
|
+
calendar = Icalendar::Calendar.new
|
|
246
|
+
calendar.add_component timezone
|
|
247
|
+
Icalendar::Calendar.parse(calendar.to_ical).first.timezones.first
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
def expect_offsets_to_match_tzinfo(tzinfo, timezone, local_times)
|
|
251
|
+
local_times.each do |local|
|
|
252
|
+
expected = tzinfo.period_for_local(local).offset.ical_offset
|
|
253
|
+
expect(timezone.offset_for_local(local).value_ical).to eq(expected), "#{tzinfo.identifier} at #{local}"
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def monthly(first, last)
|
|
258
|
+
(first.year..last.year).flat_map do |year|
|
|
259
|
+
(1..12).flat_map { |month| [DateTime.new(year, month, 1, 12), DateTime.new(year, month, 15, 12)] }
|
|
260
|
+
end.select { |local| local >= first && local <= last }
|
|
261
|
+
end
|
|
262
|
+
|
|
263
|
+
it 'matches ical_timezone when both dates are in the same period' do
|
|
264
|
+
tz = TZInfo::Timezone.get 'Europe/Brussels'
|
|
265
|
+
expect(tz.ical_timezone_for_range(DateTime.new(2021, 4, 1), DateTime.new(2021, 9, 1)).to_ical)
|
|
266
|
+
.to eq tz.ical_timezone(DateTime.new(2021, 9, 1)).to_ical
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
it 'accepts the dates in either order' do
|
|
270
|
+
tz = TZInfo::Timezone.get 'America/New_York'
|
|
271
|
+
expect(tz.ical_timezone_for_range(DateTime.new(2008, 7, 1), DateTime.new(2006, 7, 1)).to_ical)
|
|
272
|
+
.to eq tz.ical_timezone_for_range(DateTime.new(2006, 7, 1), DateTime.new(2008, 7, 1)).to_ical
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
it 'records the range it was generated for' do
|
|
276
|
+
tz = TZInfo::Timezone.get 'America/New_York'
|
|
277
|
+
source = tz.ical_timezone_for_range(DateTime.new(2006, 7, 1), DateTime.new(2008, 7, 1)).tzinfo_source
|
|
278
|
+
expect(source.identifier).to eq 'America/New_York'
|
|
279
|
+
expect([source.first, source.last]).to eq [DateTime.new(2006, 7, 1), DateTime.new(2008, 7, 1)]
|
|
280
|
+
end
|
|
281
|
+
|
|
282
|
+
context 'when a recurring rule ends east of UTC' do
|
|
283
|
+
# Sydney moved from the last Sunday in October to the first Sunday in
|
|
284
|
+
# October in 2008, and had irregular April changes in 2006 and 2008.
|
|
285
|
+
let(:tz) { TZInfo::Timezone.get 'Australia/Sydney' }
|
|
286
|
+
let(:first) { DateTime.new 2005, 7, 1, 12 }
|
|
287
|
+
let(:last) { DateTime.new 2010, 7, 1, 12 }
|
|
288
|
+
subject(:timezone) { tz.ical_timezone_for_range first, last }
|
|
289
|
+
|
|
290
|
+
it 'bounds the old rule with a UTC UNTIL at its last occurrence' do
|
|
291
|
+
rules = timezone.daylights.flat_map(&:rrule).map(&:value_ical)
|
|
292
|
+
expect(rules).to contain_exactly 'FREQ=YEARLY;BYDAY=1SU;BYMONTH=10',
|
|
293
|
+
'FREQ=YEARLY;UNTIL=20071027T160000Z;BYDAY=-1SU;BYMONTH=10'
|
|
294
|
+
end
|
|
295
|
+
|
|
296
|
+
it 'describes the correct offsets across the whole range' do
|
|
297
|
+
expect_offsets_to_match_tzinfo tz, round_trip(timezone), monthly(DateTime.new(2005, 3, 28), last)
|
|
298
|
+
end
|
|
299
|
+
end
|
|
300
|
+
|
|
301
|
+
{
|
|
302
|
+
'America/New_York' => [DateTime.new(2006, 7, 1, 12), DateTime.new(2008, 7, 1, 12)],
|
|
303
|
+
'America/Sao_Paulo' => [DateTime.new(2017, 12, 1, 12), DateTime.new(2020, 7, 1, 12)],
|
|
304
|
+
'Europe/Brussels' => [DateTime.new(2015, 1, 15, 12), DateTime.new(2025, 12, 15, 12)],
|
|
305
|
+
'America/Santiago' => [DateTime.new(2014, 1, 15, 12), DateTime.new(2024, 1, 15, 12)]
|
|
306
|
+
}.each do |identifier, (first, last)|
|
|
307
|
+
it "describes the correct offsets for #{identifier} from #{first.year} to #{last.year}" do
|
|
308
|
+
tz = TZInfo::Timezone.get identifier
|
|
309
|
+
start = tz.period_for_local(first).start_transition.local_end_at.to_datetime + 1
|
|
310
|
+
expect_offsets_to_match_tzinfo tz, round_trip(tz.ical_timezone_for_range(first, last)), monthly(start, last)
|
|
311
|
+
end
|
|
312
|
+
end
|
|
313
|
+
end
|
|
314
|
+
|
|
315
|
+
describe Icalendar::Calendar, '#add_timezone' do
|
|
316
|
+
let(:calendar) { described_class.new }
|
|
317
|
+
let(:new_york) { TZInfo::Timezone.get 'America/New_York' }
|
|
318
|
+
|
|
319
|
+
def hand_built(tzid, dtstart)
|
|
320
|
+
Icalendar::Timezone.new.tap do |tz|
|
|
321
|
+
tz.tzid = tzid
|
|
322
|
+
tz.standard do |s|
|
|
323
|
+
s.dtstart = dtstart
|
|
324
|
+
s.tzoffsetfrom = '+0100'
|
|
325
|
+
s.tzoffsetto = '+0100'
|
|
326
|
+
s.tzname = 'XST'
|
|
327
|
+
end
|
|
328
|
+
end
|
|
329
|
+
end
|
|
330
|
+
|
|
331
|
+
it 'returns the timezone the other was merged into' do
|
|
332
|
+
first = calendar.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
333
|
+
expect(calendar.add_timezone(new_york.ical_timezone(DateTime.new(2008, 7, 1)))).to equal first
|
|
334
|
+
expect(calendar.timezones).to eq [first]
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
it 'finds the merged timezone by TZID' do
|
|
338
|
+
calendar.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
339
|
+
calendar.add_timezone new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
340
|
+
found = calendar.find_timezone 'America/New_York'
|
|
341
|
+
expect(found.tzinfo_source.first).to eq DateTime.new(2006, 7, 1)
|
|
342
|
+
expect(found.tzinfo_source.last).to eq DateTime.new(2008, 7, 1)
|
|
343
|
+
expect(found.daylights.length).to eq 2
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
it 'merges timezones built with a block' do
|
|
347
|
+
calendar.timezone { |tz| tz.tzid = 'Custom/Zone'; tz.add_component hand_built('x', '19700101T000000').standards.first }
|
|
348
|
+
calendar.timezone { |tz| tz.tzid = 'Custom/Zone'; tz.add_component hand_built('x', '19800101T000000').standards.first }
|
|
349
|
+
expect(calendar.timezones.length).to eq 1
|
|
350
|
+
expect(calendar.timezones.first.standards.length).to eq 2
|
|
351
|
+
end
|
|
352
|
+
|
|
353
|
+
it 'combines the observances of hand-built timezones that share a TZID' do
|
|
354
|
+
first = calendar.add_timezone hand_built('Custom/Zone', '19700101T000000')
|
|
355
|
+
calendar.add_timezone hand_built('Custom/Zone', '19800101T000000')
|
|
356
|
+
calendar.add_timezone hand_built('Custom/Zone', '19700101T000000')
|
|
357
|
+
expect(calendar.timezones).to eq [first]
|
|
358
|
+
expect(first.standards.map { |s| s.dtstart.value_ical }).to eq %w[19700101T000000 19800101T000000]
|
|
359
|
+
expect(first.standards.map(&:parent)).to all equal(first)
|
|
360
|
+
end
|
|
361
|
+
|
|
362
|
+
it 'combines a TZInfo timezone with a hand-built one that shares its TZID' do
|
|
363
|
+
generated = calendar.add_timezone new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
364
|
+
calendar.add_timezone hand_built('America/New_York', '19700101T000000')
|
|
365
|
+
expect(calendar.timezones).to eq [generated]
|
|
366
|
+
expect(generated.standards.length).to eq 2
|
|
367
|
+
expect(generated.tzinfo_source).to be_nil
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
it 'does not merge timezones added with add_component' do
|
|
371
|
+
calendar.add_component new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
372
|
+
calendar.add_component new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
373
|
+
expect(calendar.timezones.length).to eq 2
|
|
374
|
+
end
|
|
375
|
+
|
|
376
|
+
it 'does not merge timezones appended to timezones' do
|
|
377
|
+
calendar.timezones << new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
378
|
+
calendar.timezones << new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
379
|
+
expect(calendar.timezones.length).to eq 2
|
|
380
|
+
end
|
|
381
|
+
|
|
382
|
+
it 'does not merge duplicate VTIMEZONEs when parsing' do
|
|
383
|
+
calendar.add_component new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
384
|
+
calendar.add_component new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
385
|
+
expect(described_class.parse(calendar.to_ical).first.timezones.length).to eq 2
|
|
386
|
+
end
|
|
387
|
+
|
|
388
|
+
it 'survives marshalling with its TZInfo source' do
|
|
389
|
+
calendar.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
390
|
+
loaded = Marshal.load(Marshal.dump(calendar))
|
|
391
|
+
loaded.add_timezone new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
392
|
+
expect(loaded.timezones.length).to eq 1
|
|
393
|
+
expect(loaded.timezones.first.daylights.length).to eq 2
|
|
394
|
+
end
|
|
395
|
+
end
|
|
396
|
+
|
|
397
|
+
describe Icalendar::Calendar, '#rebuild_timezones' do
|
|
398
|
+
let(:new_york) { TZInfo::Timezone.get 'America/New_York' }
|
|
399
|
+
|
|
400
|
+
def hand_built(tzid, dtstart = '19700101T000000', offset = '+0100')
|
|
401
|
+
Icalendar::Timezone.new.tap do |tz|
|
|
402
|
+
tz.tzid = tzid
|
|
403
|
+
tz.standard do |s|
|
|
404
|
+
s.dtstart = dtstart
|
|
405
|
+
s.tzoffsetfrom = offset
|
|
406
|
+
s.tzoffsetto = offset
|
|
407
|
+
s.tzname = 'XST'
|
|
408
|
+
end
|
|
409
|
+
end
|
|
410
|
+
end
|
|
411
|
+
|
|
412
|
+
def add_event(calendar, start, tzid, **props)
|
|
413
|
+
calendar.event do |e|
|
|
414
|
+
e.dtstart = Icalendar::Values::DateTime.new start, 'tzid' => tzid
|
|
415
|
+
e.dtend = Icalendar::Values::DateTime.new start + Rational(1, 24), 'tzid' => tzid
|
|
416
|
+
props.each { |name, value| e.send "#{name}=", value }
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
|
|
420
|
+
def expect_offsets_to_match_tzinfo(timezone, zone, local_times)
|
|
421
|
+
local_times.each do |local|
|
|
422
|
+
expect(timezone.offset_for_local(local).value_ical).to eq(zone.period_for_local(local).offset.ical_offset),
|
|
423
|
+
"#{zone.identifier} at #{local}"
|
|
424
|
+
end
|
|
425
|
+
end
|
|
426
|
+
|
|
427
|
+
context 'with duplicate VTIMEZONEs parsed from a file' do
|
|
428
|
+
let(:dates) { [DateTime.new(2006, 7, 1, 12), DateTime.new(2008, 7, 1, 12)] }
|
|
429
|
+
let(:calendar) do
|
|
430
|
+
source = described_class.new
|
|
431
|
+
dates.each do |date|
|
|
432
|
+
source.add_component new_york.ical_timezone(date)
|
|
433
|
+
add_event source, date, 'America/New_York'
|
|
434
|
+
end
|
|
435
|
+
described_class.parse(source.to_ical).first
|
|
436
|
+
end
|
|
437
|
+
|
|
438
|
+
it 'replaces them with one timezone covering every event' do
|
|
439
|
+
expect(calendar.timezones.length).to eq 2
|
|
440
|
+
calendar.rebuild_timezones
|
|
441
|
+
|
|
442
|
+
expect(calendar.timezones.length).to eq 1
|
|
443
|
+
timezone = calendar.timezones.first
|
|
444
|
+
expect(timezone.tzid.to_s).to eq 'America/New_York'
|
|
445
|
+
expect(timezone.parent).to equal calendar
|
|
446
|
+
expect(timezone.daylights.length).to eq 2
|
|
447
|
+
expect(timezone.standards.length).to eq 2
|
|
448
|
+
expect_offsets_to_match_tzinfo timezone, new_york, dates + [DateTime.new(2007, 3, 20, 12)]
|
|
449
|
+
end
|
|
450
|
+
|
|
451
|
+
it 'outputs a single VTIMEZONE' do
|
|
452
|
+
expect(calendar.rebuild_timezones.to_ical.scan(/^BEGIN:VTIMEZONE\r$/).length).to eq 1
|
|
453
|
+
end
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
it 'replaces an incorrect timezone whose TZID is a TZInfo identifier' do
|
|
457
|
+
calendar = described_class.new
|
|
458
|
+
calendar.add_timezone hand_built('America/New_York', '19700101T000000', '+0300')
|
|
459
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'America/New_York'
|
|
460
|
+
calendar.rebuild_timezones
|
|
461
|
+
|
|
462
|
+
timezone = calendar.timezones.first
|
|
463
|
+
expect(timezone.tzinfo_source.identifier).to eq 'America/New_York'
|
|
464
|
+
expect_offsets_to_match_tzinfo timezone, new_york, [DateTime.new(2021, 7, 1, 12)]
|
|
465
|
+
end
|
|
466
|
+
|
|
467
|
+
it 'leaves timezones whose TZID is not a TZInfo identifier as they are' do
|
|
468
|
+
calendar = described_class.new
|
|
469
|
+
first = hand_built('Custom/Zone')
|
|
470
|
+
second = hand_built('Custom/Zone', '19800101T000000')
|
|
471
|
+
calendar.timezones << first << second
|
|
472
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Custom/Zone'
|
|
473
|
+
original = calendar.to_ical
|
|
474
|
+
calendar.rebuild_timezones
|
|
475
|
+
|
|
476
|
+
expect(calendar.timezones).to eq [first, second]
|
|
477
|
+
expect(calendar.to_ical).to eq original
|
|
478
|
+
end
|
|
479
|
+
|
|
480
|
+
it 'leaves timezones that no event uses as they are' do
|
|
481
|
+
calendar = described_class.new
|
|
482
|
+
unused = new_york.ical_timezone(DateTime.new(2006, 7, 1))
|
|
483
|
+
calendar.timezones << unused
|
|
484
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Europe/Brussels'
|
|
485
|
+
calendar.rebuild_timezones
|
|
486
|
+
expect(calendar.timezones.first).to equal unused
|
|
487
|
+
expect(calendar.timezones.map { |tz| tz.tzid.to_s }).to eq ['America/New_York', 'Europe/Brussels']
|
|
488
|
+
end
|
|
489
|
+
|
|
490
|
+
it 'adds a timezone for each TZInfo identifier the events use without one' do
|
|
491
|
+
calendar = described_class.new
|
|
492
|
+
brussels = calendar.add_timezone TZInfo::Timezone.get('Europe/Brussels').ical_timezone(DateTime.new(2021, 7, 1))
|
|
493
|
+
add_event calendar, DateTime.new(2006, 7, 1, 12), 'America/New_York'
|
|
494
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Europe/Brussels'
|
|
495
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Asia/Tokyo'
|
|
496
|
+
add_event calendar, DateTime.new(2008, 7, 1, 12), 'America/New_York'
|
|
497
|
+
calendar.rebuild_timezones
|
|
498
|
+
|
|
499
|
+
expect(calendar.timezones.map { |tz| tz.tzid.to_s }).to eq ['Europe/Brussels', 'America/New_York', 'Asia/Tokyo']
|
|
500
|
+
expect(calendar.timezones.first).not_to equal brussels
|
|
501
|
+
added = calendar.find_timezone 'America/New_York'
|
|
502
|
+
expect(added.parent).to equal calendar
|
|
503
|
+
expect(added.daylights.length).to eq 2
|
|
504
|
+
expect_offsets_to_match_tzinfo added, new_york, [DateTime.new(2006, 7, 1, 12), DateTime.new(2008, 7, 1, 12)]
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
it 'does not add timezones for TZIDs that are not TZInfo identifiers' do
|
|
508
|
+
calendar = described_class.new
|
|
509
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Custom/Zone'
|
|
510
|
+
expect(calendar.rebuild_timezones.timezones).to be_empty
|
|
511
|
+
end
|
|
512
|
+
|
|
513
|
+
it 'keeps every timezone in its position' do
|
|
514
|
+
calendar = described_class.new
|
|
515
|
+
custom = hand_built('Custom/Zone')
|
|
516
|
+
calendar.timezones << new_york.ical_timezone(DateTime.new(2006, 7, 1)) << custom
|
|
517
|
+
calendar.timezones << TZInfo::Timezone.get('Europe/Brussels').ical_timezone(DateTime.new(2021, 7, 1))
|
|
518
|
+
calendar.timezones << new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
519
|
+
add_event calendar, DateTime.new(2006, 7, 1, 12), 'America/New_York'
|
|
520
|
+
add_event calendar, DateTime.new(2021, 7, 1, 12), 'Europe/Brussels'
|
|
521
|
+
calendar.rebuild_timezones
|
|
522
|
+
|
|
523
|
+
expect(calendar.timezones.map { |tz| tz.tzid.to_s }).to eq ['America/New_York', 'Custom/Zone', 'Europe/Brussels']
|
|
524
|
+
expect(calendar.timezones[1]).to equal custom
|
|
525
|
+
end
|
|
526
|
+
|
|
527
|
+
{
|
|
528
|
+
'RRULE UNTIL' => { rrule: 'FREQ=WEEKLY;UNTIL=20080701T160000Z' },
|
|
529
|
+
'RDATE' => { rdate: Icalendar::Values::DateTime.new(DateTime.new(2008, 7, 1, 12), 'tzid' => 'America/New_York') },
|
|
530
|
+
'EXDATE' => { exdate: Icalendar::Values::DateTime.new(DateTime.new(2008, 7, 1, 12), 'tzid' => 'America/New_York') },
|
|
531
|
+
'RECURRENCE-ID' => { recurrence_id: Icalendar::Values::DateTime.new(DateTime.new(2008, 7, 1, 12), 'tzid' => 'America/New_York') }
|
|
532
|
+
}.each do |name, props|
|
|
533
|
+
it "covers the dates in #{name}" do
|
|
534
|
+
calendar = described_class.new
|
|
535
|
+
calendar.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1, 12))
|
|
536
|
+
add_event calendar, DateTime.new(2006, 7, 1, 12), 'America/New_York', **props
|
|
537
|
+
calendar.rebuild_timezones
|
|
538
|
+
|
|
539
|
+
timezone = calendar.timezones.first
|
|
540
|
+
expect(timezone.tzinfo_source.last).to eq DateTime.new(2008, 7, 1, 12)
|
|
541
|
+
expect(timezone.daylights.length).to eq 2
|
|
542
|
+
end
|
|
543
|
+
end
|
|
544
|
+
|
|
545
|
+
it 'covers dates in a parsed EXDATE list' do
|
|
546
|
+
source = described_class.new
|
|
547
|
+
source.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1, 12))
|
|
548
|
+
add_event source, DateTime.new(2006, 7, 1, 12), 'America/New_York', rrule: 'FREQ=YEARLY'
|
|
549
|
+
source.events.first.append_exdate Icalendar::Values::Helpers::Array.new(
|
|
550
|
+
[DateTime.new(2007, 7, 1, 12), DateTime.new(2008, 7, 1, 12)], Icalendar::Values::DateTime, 'tzid' => 'America/New_York'
|
|
551
|
+
)
|
|
552
|
+
calendar = described_class.parse(source.to_ical).first
|
|
553
|
+
calendar.rebuild_timezones
|
|
554
|
+
expect(calendar.timezones.first.tzinfo_source.last).to eq DateTime.new(2008, 7, 1, 12)
|
|
555
|
+
end
|
|
556
|
+
|
|
557
|
+
it 'ignores dates without a TZID' do
|
|
558
|
+
calendar = described_class.new
|
|
559
|
+
calendar.add_timezone new_york.ical_timezone(DateTime.new(2006, 7, 1, 12))
|
|
560
|
+
add_event calendar, DateTime.new(2006, 7, 1, 12), 'America/New_York',
|
|
561
|
+
rdate: Icalendar::Values::DateTime.new(DateTime.new(2008, 7, 1, 12))
|
|
562
|
+
calendar.event { |e| e.dtstart = Icalendar::Values::Date.new('20100101') }
|
|
563
|
+
calendar.rebuild_timezones
|
|
564
|
+
expect(calendar.timezones.first.tzinfo_source.last).to eq DateTime.new(2006, 7, 1, 13)
|
|
565
|
+
end
|
|
566
|
+
|
|
567
|
+
it 'returns the calendar' do
|
|
568
|
+
calendar = described_class.new
|
|
569
|
+
expect(calendar.rebuild_timezones).to equal calendar
|
|
570
|
+
end
|
|
571
|
+
|
|
572
|
+
context 'when TZInfo support is not loaded' do
|
|
573
|
+
let(:calendar) do
|
|
574
|
+
described_class.new.tap do |cal|
|
|
575
|
+
cal.timezones << new_york.ical_timezone(DateTime.new(2006, 7, 1)) << new_york.ical_timezone(DateTime.new(2008, 7, 1))
|
|
576
|
+
add_event cal, DateTime.new(2006, 7, 1, 12), 'America/New_York'
|
|
577
|
+
end
|
|
578
|
+
end
|
|
579
|
+
|
|
580
|
+
it 'changes nothing without TZInfo' do
|
|
581
|
+
original = calendar.timezones.dup
|
|
582
|
+
hide_const 'TZInfo'
|
|
583
|
+
calendar.rebuild_timezones
|
|
584
|
+
expect(calendar.timezones).to eq original
|
|
585
|
+
end
|
|
586
|
+
|
|
587
|
+
it 'changes nothing without icalendar/tzinfo' do
|
|
588
|
+
original = calendar.timezones.dup
|
|
589
|
+
allow(TZInfo::Timezone).to receive(:method_defined?).and_call_original
|
|
590
|
+
allow(TZInfo::Timezone).to receive(:method_defined?).with(:ical_timezone_for_range).and_return(false)
|
|
591
|
+
calendar.rebuild_timezones
|
|
592
|
+
expect(calendar.timezones).to eq original
|
|
593
|
+
end
|
|
594
|
+
end
|
|
595
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require_relative '../lib/icalendar/tzinfo'
|
|
3
|
+
|
|
4
|
+
describe Icalendar::TransitionRuleSample do
|
|
5
|
+
def sample_transition(year, month, day = nil)
|
|
6
|
+
day ||= (2..8).find { |value| Date.new(year, month, value).wday == 0 }
|
|
7
|
+
from, to = month == 4 ? [-10800, -14400] : [-14400, -10800]
|
|
8
|
+
offset_type = Struct.new(:utc_total_offset)
|
|
9
|
+
transition_type = Struct.new(:local_end, :previous_offset, :offset) do
|
|
10
|
+
include Icalendar::TimezoneTransition
|
|
11
|
+
end
|
|
12
|
+
transition_type.new(DateTime.new(year, month, day), offset_type.new(from), offset_type.new(to))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
let(:transitions) { (2026...2054).flat_map { |year| [sample_transition(year, 4), sample_transition(year, 9)] } }
|
|
16
|
+
let(:anchors) { transitions.first(2) }
|
|
17
|
+
|
|
18
|
+
def inferred(items = transitions, references = anchors, years = 28)
|
|
19
|
+
described_class.rules(items, references, 2026, years)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
it 'infers each complete same-month seven-day window' do
|
|
23
|
+
expect(inferred).to eq([
|
|
24
|
+
['FREQ=YEARLY;BYMONTH=4;BYDAY=SU;BYMONTHDAY=2,3,4,5,6,7,8'],
|
|
25
|
+
['FREQ=YEARLY;BYMONTH=9;BYDAY=SU;BYMONTHDAY=2,3,4,5,6,7,8']
|
|
26
|
+
])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it 'preserves a validated ordinary ordinal rule' do
|
|
30
|
+
ordinary = (2026...2054).flat_map do |year|
|
|
31
|
+
[4, 9].map do |month|
|
|
32
|
+
day = (1..7).find { |value| Date.new(year, month, value).wday == 0 }
|
|
33
|
+
sample_transition(year, month, day)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
expect(inferred(ordinary, ordinary.first(2))).to eq(ordinary.first(2).map(&:rrule))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
it 'rejects a missing transition' do
|
|
40
|
+
expect(inferred(transitions[1..-1])).to be_nil
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'rejects a missing complete year' do
|
|
44
|
+
expect(inferred(transitions.reject { |item| item.local_end.year == 2032 })).to be_nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it 'rejects additional transitions even when they have unrelated offsets' do
|
|
48
|
+
extra = sample_transition(2032, 4)
|
|
49
|
+
extra.offset.utc_total_offset = -12000
|
|
50
|
+
expect(inferred(transitions + [extra])).to be_nil
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
it 'rejects a one-second change in local onset' do
|
|
54
|
+
transitions[10].local_end += Rational(1, 86400)
|
|
55
|
+
expect(inferred).to be_nil
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
it 'rejects a sub-minute offset change without using formatted offsets' do
|
|
59
|
+
transitions[10].offset.utc_total_offset += 1
|
|
60
|
+
expect(inferred).to be_nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
it 'rejects a transition crossing into another month' do
|
|
64
|
+
transitions[10].local_end = DateTime.new(2031, 3, 30)
|
|
65
|
+
expect(inferred).to be_nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
it 'rejects an exceptional year despite unchanged weekday and offsets' do
|
|
69
|
+
transitions[10].local_end += 7
|
|
70
|
+
expect(inferred).to be_nil
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'rejects incomplete seven-day coverage rather than guessing from extrema' do
|
|
74
|
+
expect(inferred(transitions.first(8), anchors, 4)).to be_nil
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'rejects fractional onset seconds' do
|
|
78
|
+
transitions[10].local_end += Rational(1, 172800)
|
|
79
|
+
expect(inferred).to be_nil
|
|
80
|
+
end
|
|
81
|
+
end
|