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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6ce9d56ed75083d2d030feb3b49abd4065eed64b4480cbd6125afb1dea602163
|
|
4
|
+
data.tar.gz: 5f20d5dde96f8e22e3e36575cb2fbd83223e6ad269bc1a777357eb016c5ba057
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 71b0ea2c251ab18a8e1143a00f8dc9d37cc04ed3598c5998bcf812d5abd75465d63d067fa0b2ff58094c2824ea4024a7685e378941928469f679f379d8469166
|
|
7
|
+
data.tar.gz: e8cd77369835bb095250ea73c35b3261ccaf853c375d9108eac269b9465e897a3ebf7e84c9e787fd1603b161301d12b3adcd760c70cfef3859a2f14301de15a6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
## Unreleased
|
|
2
2
|
|
|
3
|
+
## 2.14.0 - 2026-11-21
|
|
4
|
+
- Merge timezones with the same TZID when adding them to a calendar, so each TZID is output once with DAYLIGHT and STANDARD observances covering every added date, including rule changes between them (#205). Adds `TZInfo::Timezone#ical_timezone_for_range`. `add_timezone` now returns the timezone in the calendar, which is the existing one after a merge.
|
|
5
|
+
- Add `Calendar#rebuild_timezones` to regenerate timezones with TZInfo identifiers from the dates the calendar's events use them for, adding any that are missing, such as to replace duplicate VTIMEZONEs parsed from a file.
|
|
6
|
+
- Evaluate a timezone observance's RRULE UNTIL as UTC, so its last occurrence east of UTC is not dropped.
|
|
7
|
+
- Generate correct timezone RRULEs for timezones with rules like Sun >= 2 - sunxiuguo
|
|
8
|
+
|
|
3
9
|
## 2.13.0 - 2026-10-11
|
|
4
10
|
- Generate `CATEGORIES` as a single comma-separated line instead of one line per value, matching how Google Calendar and Nextcloud expect it (#226). Setting `categories = [...]` now stores the joined value as one item, so code that reads `#categories` expecting one entry per category will see a single combined entry instead; `append_categories` is unaffected and still adds a separate line. - Carlos Kvasir
|
|
5
11
|
- Replace OpenStruct with Structs in Duration, Recur and UtcOffset values and drop the ostruct dependency (#329). Requires Ruby 2.5 or newer, for `Struct`'s `keyword_init:`. The parts keep the same readers and setters, but `#value` is now a Struct, so assigning an attribute that isn't one of its parts raises `NoMethodError`. - David Corson-Knowles
|
data/README.md
CHANGED
|
@@ -248,6 +248,43 @@ cal.event do |e|
|
|
|
248
248
|
end
|
|
249
249
|
```
|
|
250
250
|
|
|
251
|
+
#### Events in more than one period ####
|
|
252
|
+
|
|
253
|
+
`ical_timezone` only describes the timezone around the date it was given. When a calendar's
|
|
254
|
+
events span daylight saving changes or changes to a timezone's rules, add the timezone for each
|
|
255
|
+
event. `add_timezone` (and `timezone`) merge timezones with the same TZID into the one already
|
|
256
|
+
in the calendar, so each TZID is output once with observances covering every event:
|
|
257
|
+
|
|
258
|
+
```ruby
|
|
259
|
+
events.each do |event|
|
|
260
|
+
cal.add_timezone tz.ical_timezone(event.start_time)
|
|
261
|
+
end
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
The single timezone can also be generated directly for a range of dates:
|
|
265
|
+
|
|
266
|
+
```ruby
|
|
267
|
+
cal.add_timezone tz.ical_timezone_for_range(first_event_start, last_event_end)
|
|
268
|
+
```
|
|
269
|
+
|
|
270
|
+
Hand-built timezones that share a TZID have their `DAYLIGHT` and `STANDARD` observances combined.
|
|
271
|
+
Timezones added with `add_component` or appended to `cal.timezones` (as the parser does) are not merged.
|
|
272
|
+
|
|
273
|
+
`rebuild_timezones` regenerates a calendar's timezones from the dates its events use them for,
|
|
274
|
+
for example to replace the duplicate or out-of-date VTIMEZONEs of a parsed file:
|
|
275
|
+
|
|
276
|
+
```ruby
|
|
277
|
+
require 'icalendar/tzinfo'
|
|
278
|
+
|
|
279
|
+
cal = Icalendar::Calendar.parse(File.read('calendar.ics')).first
|
|
280
|
+
cal.rebuild_timezones
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
Each timezone whose TZID is a TZInfo identifier is replaced by one generated to cover the event
|
|
284
|
+
`DTSTART`, `DTEND`, `RECURRENCE-ID`, `RDATE`, `EXDATE` and `RRULE` `UNTIL` dates with that TZID.
|
|
285
|
+
A timezone is also added for each TZInfo identifier the events use that the calendar has no
|
|
286
|
+
timezone for. Timezones with any other TZID, or that no event uses, are left as they are.
|
|
287
|
+
|
|
251
288
|
|
|
252
289
|
Parsing iCalendars
|
|
253
290
|
---
|
data/icalendar.gemspec
CHANGED
|
@@ -51,6 +51,7 @@ ActiveSupport is required for TimeWithZone support, but not required for general
|
|
|
51
51
|
# s.add_development_dependency 'tzinfo', '~> 0.3'
|
|
52
52
|
# end tzinfo
|
|
53
53
|
|
|
54
|
+
s.add_development_dependency 'irb'
|
|
54
55
|
s.add_development_dependency 'timecop', '~> 0.9'
|
|
55
56
|
s.add_development_dependency 'rspec', '~> 3.8'
|
|
56
57
|
s.add_development_dependency 'simplecov', '~> 0.16'
|
data/lib/icalendar/calendar.rb
CHANGED
|
@@ -71,6 +71,104 @@ module Icalendar
|
|
|
71
71
|
self
|
|
72
72
|
end
|
|
73
73
|
|
|
74
|
+
module MergesTimezones
|
|
75
|
+
def timezone(tz = nil)
|
|
76
|
+
tz ||= Icalendar::Timezone.new
|
|
77
|
+
tz.parent = self
|
|
78
|
+
yield tz if block_given?
|
|
79
|
+
return tz if timezones.any? { |existing| existing.equal? tz }
|
|
80
|
+
|
|
81
|
+
existing = timezones.find { |candidate| !tz.tzid.nil? && candidate.tzid.to_s == tz.tzid.to_s }
|
|
82
|
+
existing ? existing.merge!(tz) : add_component(tz)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def rebuild_timezones
|
|
86
|
+
return self unless defined?(TZInfo::Timezone) &&
|
|
87
|
+
TZInfo::Timezone.method_defined?(:ical_timezone_for_range)
|
|
88
|
+
|
|
89
|
+
dates = event_dates_by_tzid
|
|
90
|
+
rebuilt = {}
|
|
91
|
+
kept = timezones.each_with_object([]) do |tz, list|
|
|
92
|
+
tzid = tz.tzid.nil? ? nil : tz.tzid.to_s
|
|
93
|
+
next list << tz unless dates.key?(tzid)
|
|
94
|
+
next if rebuilt.key?(tzid)
|
|
95
|
+
|
|
96
|
+
zone = tzinfo_timezone(tzid)
|
|
97
|
+
next list << tz if zone.nil?
|
|
98
|
+
|
|
99
|
+
list << (rebuilt[tzid] = timezone_for_dates(zone, dates[tzid]))
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# TZIDs used by events without a timezone in the calendar
|
|
103
|
+
present = timezones.map { |tz| tz.tzid.to_s unless tz.tzid.nil? }
|
|
104
|
+
dates.each do |tzid, tz_dates|
|
|
105
|
+
next if present.include?(tzid)
|
|
106
|
+
zone = tzinfo_timezone(tzid)
|
|
107
|
+
kept << timezone_for_dates(zone, tz_dates) unless zone.nil?
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
timezones.replace kept
|
|
111
|
+
self
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
prepend MergesTimezones
|
|
115
|
+
|
|
116
|
+
private
|
|
117
|
+
|
|
118
|
+
# Local wall-clock times for each TZID used by the events. An RRULE UNTIL
|
|
119
|
+
# in UTC is a lambda converting it to local time once the zone is known.
|
|
120
|
+
def event_dates_by_tzid
|
|
121
|
+
events.each_with_object(Hash.new { |hash, key| hash[key] = [] }) do |event, dates|
|
|
122
|
+
values = [event.dtstart, event.dtend, event.recurrence_id, *event.rdate, *event.exdate]
|
|
123
|
+
each_date_value(values) do |value|
|
|
124
|
+
tzid = Array(value.ical_params['tzid']).first
|
|
125
|
+
dates[tzid.to_s] << wall_clock(value.to_datetime) unless tzid.nil?
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
tzid = event.dtstart && Array(event.dtstart.ical_params['tzid']).first
|
|
129
|
+
next if tzid.nil?
|
|
130
|
+
event.rrule.each do |rule|
|
|
131
|
+
next if rule.until.nil?
|
|
132
|
+
dates[tzid.to_s] << rrule_until(rule.until)
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def timezone_for_dates(zone, dates)
|
|
138
|
+
locals = dates.map { |date| date.respond_to?(:call) ? date.call(zone) : date }
|
|
139
|
+
zone.ical_timezone_for_range(locals.min, locals.max).tap { |tz| tz.parent = self }
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def each_date_value(values, &block)
|
|
143
|
+
values.each do |value|
|
|
144
|
+
if value.is_a?(Icalendar::Values::Helpers::Array)
|
|
145
|
+
each_date_value value.value, &block
|
|
146
|
+
elsif value.is_a?(Icalendar::Value)
|
|
147
|
+
yield value
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def rrule_until(value)
|
|
153
|
+
parsed = Icalendar::Values::DateOrDateTime.new(value).call
|
|
154
|
+
if parsed.is_a?(Icalendar::Values::Date)
|
|
155
|
+
date = parsed.value
|
|
156
|
+
return ::DateTime.new(date.year, date.month, date.day, 23, 59, 59)
|
|
157
|
+
end
|
|
158
|
+
return wall_clock(parsed.to_datetime) unless value.end_with?('Z')
|
|
159
|
+
->(zone) { wall_clock(zone.utc_to_local(parsed.to_time.utc).to_datetime) }
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def wall_clock(datetime)
|
|
163
|
+
::DateTime.new(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec)
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def tzinfo_timezone(identifier)
|
|
167
|
+
TZInfo::Timezone.get identifier
|
|
168
|
+
rescue TZInfo::InvalidTimezoneIdentifier
|
|
169
|
+
nil
|
|
170
|
+
end
|
|
171
|
+
|
|
74
172
|
end
|
|
75
173
|
|
|
76
174
|
end
|
data/lib/icalendar/timezone.rb
CHANGED
|
@@ -25,7 +25,7 @@ module Icalendar
|
|
|
25
25
|
def occurrences
|
|
26
26
|
@occurrences ||= IceCube::Schedule.new(dtstart.to_time) do |s|
|
|
27
27
|
rrule.each do |rule|
|
|
28
|
-
s.add_recurrence_rule IceCube::Rule.from_ical(rule.value_ical)
|
|
28
|
+
s.add_recurrence_rule floating_until(IceCube::Rule.from_ical(rule.value_ical))
|
|
29
29
|
end
|
|
30
30
|
rdate.each do |date|
|
|
31
31
|
s.add_recurrence_time date.to_time
|
|
@@ -47,6 +47,19 @@ module Icalendar
|
|
|
47
47
|
|
|
48
48
|
@cached_occurrences.reverse_each.find { |occurrence| occurrence < from }
|
|
49
49
|
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# An observance's DTSTART is a local time in TZOFFSETFROM, but is
|
|
54
|
+
# evaluated here as a floating time. UNTIL is in UTC, so move it into the
|
|
55
|
+
# same frame, or occurrences east of UTC would be cut off early.
|
|
56
|
+
def floating_until(rule)
|
|
57
|
+
return rule if rule.until_time.nil?
|
|
58
|
+
offset = tzoffsetfrom.value
|
|
59
|
+
seconds = offset.hours * 3600 + offset.minutes * 60 + offset.seconds
|
|
60
|
+
seconds = -seconds if tzoffsetfrom.behind?
|
|
61
|
+
rule.until(rule.until_time + seconds)
|
|
62
|
+
end
|
|
50
63
|
end
|
|
51
64
|
class Daylight < Component
|
|
52
65
|
include Marshable
|
|
@@ -66,6 +79,10 @@ module Icalendar
|
|
|
66
79
|
end
|
|
67
80
|
|
|
68
81
|
|
|
82
|
+
# Records the TZInfo zone and local date range a timezone was generated
|
|
83
|
+
# for, so that timezones with the same TZID can be regenerated as one.
|
|
84
|
+
TzinfoSource = Struct.new(:identifier, :first, :last, :dst, keyword_init: true)
|
|
85
|
+
|
|
69
86
|
required_property :tzid
|
|
70
87
|
|
|
71
88
|
optional_single_property :last_modified, Icalendar::Values::DateTime
|
|
@@ -74,10 +91,39 @@ module Icalendar
|
|
|
74
91
|
component :daylight, false, Icalendar::Timezone::Daylight
|
|
75
92
|
component :standard, false, Icalendar::Timezone::Standard
|
|
76
93
|
|
|
94
|
+
attr_accessor :tzinfo_source
|
|
95
|
+
|
|
77
96
|
def initialize
|
|
78
97
|
super 'timezone'
|
|
79
98
|
end
|
|
80
99
|
|
|
100
|
+
# Combine another timezone with the same TZID into this one.
|
|
101
|
+
#
|
|
102
|
+
# When both were generated by TZInfo for the same zone, the observances are
|
|
103
|
+
# regenerated to cover every date either one was generated for. Otherwise
|
|
104
|
+
# the other timezone's observances are added, skipping identical ones.
|
|
105
|
+
def merge!(other)
|
|
106
|
+
return self if other.equal?(self)
|
|
107
|
+
|
|
108
|
+
if tzinfo_mergeable?(other)
|
|
109
|
+
source = tzinfo_source
|
|
110
|
+
first = [source.first, other.tzinfo_source.first].min
|
|
111
|
+
last = [source.last, other.tzinfo_source.last].max
|
|
112
|
+
return self if first == source.first && last == source.last
|
|
113
|
+
|
|
114
|
+
rebuilt = TZInfo::Timezone.get(source.identifier).ical_timezone_for_range(first, last, source.dst)
|
|
115
|
+
replace_observances rebuilt.daylights + rebuilt.standards
|
|
116
|
+
self.tzinfo_source = rebuilt.tzinfo_source
|
|
117
|
+
else
|
|
118
|
+
existing = (daylights + standards).map(&:to_ical)
|
|
119
|
+
(other.daylights + other.standards).each do |observance|
|
|
120
|
+
add_component observance unless existing.include?(observance.to_ical)
|
|
121
|
+
end
|
|
122
|
+
self.tzinfo_source = nil
|
|
123
|
+
end
|
|
124
|
+
self
|
|
125
|
+
end
|
|
126
|
+
|
|
81
127
|
def valid?(strict = false)
|
|
82
128
|
daylights.empty? && standards.empty? and return false
|
|
83
129
|
daylights.all? { |d| d.valid? strict } or return false
|
|
@@ -121,5 +167,19 @@ module Icalendar
|
|
|
121
167
|
end.compact
|
|
122
168
|
possible.sort_by(&:first).last
|
|
123
169
|
end
|
|
170
|
+
|
|
171
|
+
private
|
|
172
|
+
|
|
173
|
+
def tzinfo_mergeable?(other)
|
|
174
|
+
tzinfo_source && other.tzinfo_source &&
|
|
175
|
+
tzinfo_source.identifier == other.tzinfo_source.identifier &&
|
|
176
|
+
defined?(TZInfo::Timezone) && TZInfo::Timezone.method_defined?(:transitions_up_to)
|
|
177
|
+
end
|
|
178
|
+
|
|
179
|
+
def replace_observances(observances)
|
|
180
|
+
daylights.clear
|
|
181
|
+
standards.clear
|
|
182
|
+
observances.each { |observance| add_component observance }
|
|
183
|
+
end
|
|
124
184
|
end
|
|
125
185
|
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'ice_cube'
|
|
4
|
+
|
|
5
|
+
module Icalendar
|
|
6
|
+
# Adds the observances for a list of historic TZInfo transitions to a
|
|
7
|
+
# timezone whose current (indefinitely recurring) observances are already
|
|
8
|
+
# built.
|
|
9
|
+
#
|
|
10
|
+
# Transitions are walked newest to oldest. A transition that is the previous
|
|
11
|
+
# occurrence of a recurring observance of the same kind, with the same
|
|
12
|
+
# offsets and name, moves that observance's DTSTART back to it. Any other
|
|
13
|
+
# transition starts a new observance. Observances started this way recur
|
|
14
|
+
# only until the last transition they cover, or not at all when they cover
|
|
15
|
+
# a single transition.
|
|
16
|
+
class TimezoneHistory
|
|
17
|
+
Run = Struct.new(:observance, :onset, :latest, :count, keyword_init: true)
|
|
18
|
+
|
|
19
|
+
def initialize(timezone, transitions)
|
|
20
|
+
@timezone = timezone
|
|
21
|
+
@transitions = transitions
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def apply
|
|
25
|
+
open = {}
|
|
26
|
+
@timezone.daylights.each { |observance| open[:daylight] = current_run(observance) }
|
|
27
|
+
@timezone.standards.each { |observance| open[:standard] = current_run(observance) }
|
|
28
|
+
historic = []
|
|
29
|
+
|
|
30
|
+
@transitions.reverse_each do |transition|
|
|
31
|
+
kind = transition.offset.dst? ? :daylight : :standard
|
|
32
|
+
run = open[kind]
|
|
33
|
+
if run && extends?(run, transition)
|
|
34
|
+
run.observance.dtstart = transition.dtstart
|
|
35
|
+
run.onset = TransitionRuleSample.onset(transition)
|
|
36
|
+
run.count += 1
|
|
37
|
+
else
|
|
38
|
+
run = Run.new(observance: observance_for(kind, transition), onset: TransitionRuleSample.onset(transition),
|
|
39
|
+
latest: transition, count: 1)
|
|
40
|
+
open[kind] = run
|
|
41
|
+
historic << run
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
historic.reverse_each do |run|
|
|
46
|
+
observance = run.observance
|
|
47
|
+
observance.rrule = if run.count == 1
|
|
48
|
+
[]
|
|
49
|
+
else
|
|
50
|
+
"#{observance.rrule.first.value_ical};UNTIL=#{run.latest.at.to_time.utc.strftime('%Y%m%dT%H%M%SZ')}"
|
|
51
|
+
end
|
|
52
|
+
@timezone.add_component observance
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
private
|
|
57
|
+
|
|
58
|
+
def current_run(observance)
|
|
59
|
+
return if observance.rrule.empty?
|
|
60
|
+
Run.new(observance: observance, onset: observance.dtstart.value.to_datetime, count: 1)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def observance_for(kind, transition)
|
|
64
|
+
observance = kind == :daylight ? Timezone::Daylight.new : Timezone::Standard.new
|
|
65
|
+
observance.tzname = transition.offset_abbreviation
|
|
66
|
+
observance.tzoffsetfrom = transition.offset_from
|
|
67
|
+
observance.tzoffsetto = transition.offset_to
|
|
68
|
+
observance.dtstart = transition.dtstart
|
|
69
|
+
observance.rrule = transition.rrule
|
|
70
|
+
observance
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Whether this transition is an occurrence of the observance's rule, and
|
|
74
|
+
# the rule's next occurrence after it is the observance's current DTSTART.
|
|
75
|
+
def extends?(run, transition)
|
|
76
|
+
observance = run.observance
|
|
77
|
+
return false unless observance.tzoffsetfrom.value_ical == transition.offset_from &&
|
|
78
|
+
observance.tzoffsetto.value_ical == transition.offset_to &&
|
|
79
|
+
observance.tzname.first.to_s == transition.offset_abbreviation
|
|
80
|
+
|
|
81
|
+
onset = floating_time(TransitionRuleSample.onset(transition))
|
|
82
|
+
# IceCube always yields the schedule start, so start the schedule before
|
|
83
|
+
# the onset's year at the onset's clock time and look at what follows.
|
|
84
|
+
start = Time.utc(onset.year - 1, 12, 31, onset.hour, onset.min, onset.sec)
|
|
85
|
+
schedule = IceCube::Schedule.new(start)
|
|
86
|
+
schedule.add_recurrence_rule IceCube::Rule.from_ical(observance.rrule.first.value_ical)
|
|
87
|
+
schedule.next_occurrences(2, start) == [onset, floating_time(run.onset)]
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def floating_time(datetime)
|
|
91
|
+
Time.utc(datetime.year, datetime.month, datetime.day, datetime.hour, datetime.min, datetime.sec)
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Icalendar
|
|
4
|
+
# Conservative internal inference for stable, twice-yearly transitions.
|
|
5
|
+
# This is evidence from the installed timezone data, not a prediction of law.
|
|
6
|
+
class TransitionRuleSample
|
|
7
|
+
def self.onset(transition)
|
|
8
|
+
(transition.respond_to?(:local_end_at) ? transition.local_end_at : transition.local_end).to_datetime
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.signature(transition)
|
|
12
|
+
date = onset(transition)
|
|
13
|
+
[date.month, date.wday, date.hour, date.min, date.sec, date.sec_fraction,
|
|
14
|
+
transition.previous_offset.utc_total_offset, transition.offset.utc_total_offset]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def self.rules(transitions, anchors, first_year, year_count)
|
|
18
|
+
years = (first_year...(first_year + year_count)).to_a
|
|
19
|
+
# Do not discard transitions with unexpected offsets, months or clocks.
|
|
20
|
+
# Every transition in the complete local-year interval must be accounted for.
|
|
21
|
+
grouped = transitions.group_by { |transition| onset(transition).year }
|
|
22
|
+
return unless grouped.keys.sort == years
|
|
23
|
+
return unless grouped.values.all? { |items| items.length == 2 }
|
|
24
|
+
signatures = anchors.map { |anchor| signature(anchor) }
|
|
25
|
+
return unless signatures.uniq.length == 2
|
|
26
|
+
return unless signatures[0][-2, 2] == signatures[1][-2, 2].reverse
|
|
27
|
+
return if signatures[0][-2] == signatures[0][-1]
|
|
28
|
+
return unless grouped.values.all? { |items| items.map { |item| signature(item) }.sort == signatures.sort }
|
|
29
|
+
|
|
30
|
+
rules = anchors.map do |anchor|
|
|
31
|
+
dates = years.map do |year|
|
|
32
|
+
onset(grouped.fetch(year).find { |item| signature(item) == signature(anchor) })
|
|
33
|
+
end
|
|
34
|
+
start = onset(anchor)
|
|
35
|
+
week_number = (start.day - 1) / 7 + 1
|
|
36
|
+
week_number = -1 if week_number > 3
|
|
37
|
+
ordinal_days = years.map do |year|
|
|
38
|
+
days = (1..Date.new(year, start.month, -1).day).select do |day|
|
|
39
|
+
Date.new(year, start.month, day).wday == start.wday
|
|
40
|
+
end
|
|
41
|
+
days[week_number == -1 ? -1 : week_number - 1]
|
|
42
|
+
end
|
|
43
|
+
if dates.map(&:day) == ordinal_days
|
|
44
|
+
anchor.rrule
|
|
45
|
+
else
|
|
46
|
+
days = dates.map(&:day).uniq.sort
|
|
47
|
+
# Seeing all seven outcomes avoids inferring a window from sparse data.
|
|
48
|
+
return unless days.length == 7 && days == (days.first..(days.first + 6)).to_a
|
|
49
|
+
# Matching signatures already fix the month and weekday for every
|
|
50
|
+
# date. Seven consecutive observed monthdays therefore identify the
|
|
51
|
+
# unique matching weekday in the window for every sampled year.
|
|
52
|
+
["FREQ=YEARLY;BYMONTH=#{start.month};BYDAY=#{start.strftime('%a').upcase[0, 2]};BYMONTHDAY=#{days.join(',')}"]
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
rules
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
data/lib/icalendar/tzinfo.rb
CHANGED
|
@@ -42,6 +42,8 @@
|
|
|
42
42
|
|
|
43
43
|
|
|
44
44
|
require 'tzinfo'
|
|
45
|
+
require_relative 'transition_rule_sample'
|
|
46
|
+
require_relative 'timezone_history'
|
|
45
47
|
begin
|
|
46
48
|
require 'tzinfo/data'
|
|
47
49
|
rescue LoadError
|
|
@@ -96,7 +98,41 @@ end
|
|
|
96
98
|
module TZInfo
|
|
97
99
|
class Timezone
|
|
98
100
|
def ical_timezone(date, dst = Timezone.default_dst)
|
|
99
|
-
|
|
101
|
+
local = ical_local(date)
|
|
102
|
+
ical_timezone_for_period(period_for_local(local, dst)).tap do |timezone|
|
|
103
|
+
timezone.tzinfo_source = Icalendar::Timezone::TzinfoSource.new(
|
|
104
|
+
identifier: identifier, first: local, last: local, dst: dst
|
|
105
|
+
)
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def ical_timezone_for_range(first_date, last_date, dst = Timezone.default_dst)
|
|
110
|
+
first, last = [ical_local(first_date), ical_local(last_date)].sort
|
|
111
|
+
timezone = ical_timezone(last, dst)
|
|
112
|
+
timezone.tzinfo_source.first = first
|
|
113
|
+
|
|
114
|
+
first_period = period_for_local(first, dst)
|
|
115
|
+
last_period = period_for_local(last, dst)
|
|
116
|
+
return timezone if first_period == last_period || last_period.start_transition.nil?
|
|
117
|
+
# TZInfo 0.3 has no transitions_up_to; only the last period is described.
|
|
118
|
+
return timezone unless respond_to?(:transitions_up_to)
|
|
119
|
+
|
|
120
|
+
# Times before the zone's first transition are not described.
|
|
121
|
+
lower = (first_period.start_transition || first_period.end_transition).at
|
|
122
|
+
historic = transitions_up_to(last_period.start_transition.at, lower)
|
|
123
|
+
Icalendar::TimezoneHistory.new(timezone, historic).apply
|
|
124
|
+
timezone
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
private
|
|
128
|
+
|
|
129
|
+
# Wall-clock local time, without any UTC offset the caller's value carries.
|
|
130
|
+
def ical_local(date)
|
|
131
|
+
date = date.to_datetime
|
|
132
|
+
DateTime.new(date.year, date.month, date.day, date.hour, date.min, date.sec + date.sec_fraction)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def ical_timezone_for_period(period)
|
|
100
136
|
timezone = Icalendar::Timezone.new
|
|
101
137
|
timezone.tzid = identifier
|
|
102
138
|
if period.start_transition.nil?
|
|
@@ -104,8 +140,34 @@ module TZInfo
|
|
|
104
140
|
elsif period.end_transition.nil?
|
|
105
141
|
timezone.add_component period.dst? ? period.daylight : period.standard
|
|
106
142
|
else
|
|
107
|
-
|
|
108
|
-
|
|
143
|
+
daylight = period.daylight
|
|
144
|
+
standard = period.standard
|
|
145
|
+
# TZInfo 0.3 has no transitions_up_to; retain its existing behavior.
|
|
146
|
+
if respond_to?(:transitions_up_to)
|
|
147
|
+
anchors = [period.dst? ? period.start_transition : period.end_transition,
|
|
148
|
+
period.dst? ? period.end_transition : period.start_transition]
|
|
149
|
+
first_year = anchors.map { |anchor| Icalendar::TransitionRuleSample.onset(anchor).year }.min
|
|
150
|
+
# Away from non-leap century years, weekday/leap-year patterns repeat
|
|
151
|
+
# every 28 years. This bounded sample does not predict timezone-law
|
|
152
|
+
# changes; window rules still require all seven monthday outcomes.
|
|
153
|
+
year_count = 28
|
|
154
|
+
# transitions_up_to takes UTC bounds, while recurrence onsets use the
|
|
155
|
+
# previous offset's local time. Pad the query to include onsets near
|
|
156
|
+
# local New Year, then discard only the padding outside our local years.
|
|
157
|
+
transitions = transitions_up_to(Time.utc(first_year + year_count, 1, 3),
|
|
158
|
+
Time.utc(first_year - 1, 12, 30))
|
|
159
|
+
transitions = transitions.select do |transition|
|
|
160
|
+
year = Icalendar::TransitionRuleSample.onset(transition).year
|
|
161
|
+
year >= first_year && year < first_year + year_count
|
|
162
|
+
end
|
|
163
|
+
rules = Icalendar::TransitionRuleSample.rules(transitions, anchors, first_year, year_count)
|
|
164
|
+
if rules
|
|
165
|
+
daylight.rrule = rules[0]
|
|
166
|
+
standard.rrule = rules[1]
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
timezone.add_component daylight
|
|
170
|
+
timezone.add_component standard
|
|
109
171
|
end
|
|
110
172
|
timezone
|
|
111
173
|
end
|