expand.rs
⎇
Raw
1//! Recurrence expansion: DTSTART + RRULE + RDATE - EXRULE - EXDATE, with
2//! RECURRENCE-ID overrides applied.
3
4use std::collections::{BTreeMap, HashMap, HashSet};
5use std::ops::Range;
6
7use calcard::common::{CalendarScale, PartialDateTime};
8use calcard::icalendar::{
9 ICalendar, ICalendarComponent, ICalendarComponentType, ICalendarDuration, ICalendarFrequency,
10 ICalendarParameterName, ICalendarPeriod, ICalendarProperty, ICalendarRecurrenceRule,
11 ICalendarSkip, ICalendarValue,
12};
13use chrono::{DateTime, NaiveDateTime, NaiveTime, TimeDelta, TimeZone, Utc, Weekday};
14
15use crate::zone::{Zone, Zones, add, add_local};
16
17/// Occurrences one rule may generate before expansion gives up.
18// ponytail: a FREQ=MINUTELY rule that started years ago hits this. Skip ahead
19// for simple rules if that matters.
20const MAX_OCCURRENCES: usize = 1_000_000;
21
22#[derive(Debug, Clone, PartialEq)]
23pub struct Instance {
24 pub start: DateTime<Utc>,
25 pub end: DateTime<Utc>,
26 /// The original start. `None` if the component does not recur.
27 pub recurrence_id: Option<DateTime<Utc>>,
28 /// Index into `ICalendar::components` of the master or override that
29 /// describes this instance.
30 pub component: usize,
31}
32
33#[derive(Debug, Default)]
34pub struct Expansion {
35 /// Sorted by start.
36 pub instances: Vec<Instance>,
37 /// A rule reached `MAX_OCCURRENCES`, so later instances are missing.
38 pub truncated: bool,
39}
40
41/// The instances of all VEVENT, VTODO and VJOURNAL components that overlap
42/// `window`. A zero-length instance overlaps if it starts inside.
43///
44/// Components are grouped by type and UID into one master and its overrides.
45/// Components without DTSTART are skipped. `floating` interprets values
46/// without a zone.
47pub fn expand(cal: &ICalendar, window: Range<DateTime<Utc>>, floating: Zone) -> Expansion {
48 let zones = Zones::new(cal, floating);
49 let mut out = Expansion::default();
50 let mut groups: HashMap<(&ICalendarComponentType, &str), Vec<usize>> = HashMap::new();
51 for (i, c) in cal.components.iter().enumerate() {
52 if !matches!(
53 c.component_type,
54 ICalendarComponentType::VEvent
55 | ICalendarComponentType::VTodo
56 | ICalendarComponentType::VJournal
57 ) {
58 continue;
59 }
60 match c.uid() {
61 Some(uid) => groups.entry((&c.component_type, uid)).or_default().push(i),
62 None => expand_group(cal, &zones, &[i], &window, &mut out),
63 }
64 }
65 for group in groups.values() {
66 expand_group(cal, &zones, group, &window, &mut out);
67 }
68 out.instances.sort_by_key(|i| (i.start, i.component));
69 out
70}
71
72/// A date or date-time value and the zone it is in.
73#[derive(Debug, Clone)]
74struct Stamp {
75 local: NaiveDateTime,
76 zone: Zone,
77 date: bool,
78 /// A date-time without TZID or UTC offset.
79 floating: bool,
80}
81
82impl Stamp {
83 fn utc(&self) -> DateTime<Utc> {
84 self.zone.to_utc(self.local)
85 }
86}
87
88#[derive(Debug, Clone)]
89enum Length {
90 Exact(TimeDelta),
91 /// Whole days count in wall-clock time, so a day can last 23 or 25 hours.
92 Nominal {
93 days: i64,
94 exact: TimeDelta,
95 },
96}
97
98impl Length {
99 fn end(&self, zone: &Zone, local: NaiveDateTime, utc: DateTime<Utc>) -> DateTime<Utc> {
100 match self {
101 Length::Exact(d) => add(utc, *d),
102 Length::Nominal { days, exact } => add(
103 zone.to_utc(add_local(local, TimeDelta::days(*days))),
104 *exact,
105 ),
106 }
107 }
108
109 /// An upper bound, for widening the generation window.
110 fn max(&self) -> TimeDelta {
111 match self {
112 Length::Exact(d) => *d,
113 Length::Nominal { days, exact } => TimeDelta::days(days + 1) + *exact,
114 }
115 }
116}
117
118/// DTSTART and length of one component.
119#[derive(Debug, Clone)]
120struct Timing {
121 start: Stamp,
122 length: Length,
123 component: usize,
124}
125
126impl Timing {
127 fn of(cal: &ICalendar, zones: &Zones, component: usize) -> Option<Self> {
128 let c = &cal.components[component];
129 let start = prop_stamp(zones, c, &ICalendarProperty::Dtstart)?;
130 let end = match c.component_type {
131 ICalendarComponentType::VTodo => ICalendarProperty::Due,
132 _ => ICalendarProperty::Dtend,
133 };
134 let zero = TimeDelta::zero();
135 let length = if let Some(end) = prop_stamp(zones, c, &end) {
136 if start.date && end.date {
137 Length::Nominal {
138 days: (end.local - start.local).num_days().max(0),
139 exact: zero,
140 }
141 } else {
142 Length::Exact((end.utc() - start.utc()).max(zero))
143 }
144 } else if let Some(ICalendarValue::Duration(d)) = c
145 .property(&ICalendarProperty::Duration)
146 .and_then(|e| e.values.first())
147 {
148 nominal(d)
149 } else if start.date {
150 Length::Nominal {
151 days: 1,
152 exact: zero,
153 }
154 } else {
155 Length::Exact(zero)
156 };
157 Some(Timing {
158 start,
159 length,
160 component,
161 })
162 }
163
164 fn end_at(&self, local: NaiveDateTime, utc: DateTime<Utc>) -> DateTime<Utc> {
165 self.length.end(&self.start.zone, local, utc)
166 }
167
168 /// A RECURRENCE-ID, RDATE or EXDATE value. Dates and floating times are
169 /// read in the zone of the series: clients write them that way.
170 fn utc_of(&self, s: &Stamp) -> DateTime<Utc> {
171 if s.date || s.floating {
172 self.start.zone.to_utc(s.local)
173 } else {
174 s.utc()
175 }
176 }
177
178 /// The identity of a recurrence instance. An all-day series matches by
179 /// date alone, whatever zone the other value names.
180 fn key(&self, s: &Stamp) -> i64 {
181 if self.start.date {
182 day_key(s.local)
183 } else {
184 self.utc_of(s).timestamp()
185 }
186 }
187
188 fn member_key(&self, local: NaiveDateTime, utc: DateTime<Utc>) -> i64 {
189 if self.start.date {
190 day_key(local)
191 } else {
192 utc.timestamp()
193 }
194 }
195}
196
197struct Member {
198 local: NaiveDateTime,
199 utc: DateTime<Utc>,
200 /// From an RDATE period.
201 length: Option<Length>,
202}
203
204struct Override {
205 sequence: i64,
206 recurrence_id: DateTime<Utc>,
207 timing: Timing,
208}
209
210fn expand_group(
211 cal: &ICalendar,
212 zones: &Zones,
213 group: &[usize],
214 window: &Range<DateTime<Utc>>,
215 out: &mut Expansion,
216) {
217 let comp = |i: usize| &cal.components[i];
218 let sequence = |i: usize| {
219 comp(i)
220 .property(&ICalendarProperty::Sequence)
221 .and_then(|e| e.values.first()?.as_integer())
222 .unwrap_or(0)
223 };
224 let (overrides, masters): (Vec<usize>, Vec<usize>) = group
225 .iter()
226 .partition(|&&i| comp(i).has_property(&ICalendarProperty::RecurrenceId));
227 // Several masters for one UID are invalid. Take the latest revision,
228 // or the first one on a tie.
229 let master = masters
230 .into_iter()
231 .rev()
232 .max_by_key(|&i| sequence(i))
233 .and_then(|i| Timing::of(cal, zones, i));
234
235 // Matched by RECURRENCE-ID only. SEQUENCE picks between two overrides of
236 // one instance and never decides whether an override applies.
237 let mut exact: HashMap<i64, Override> = HashMap::new();
238 let mut future: Vec<(i64, DateTime<Utc>, Timing)> = Vec::new();
239 for i in overrides {
240 let Some(e) = comp(i).property(&ICalendarProperty::RecurrenceId) else {
241 continue;
242 };
243 let (Some(timing), Some(rid)) = (
244 Timing::of(cal, zones, i),
245 e.values
246 .first()
247 .and_then(|v| stamp(zones, v.as_partial_date_time()?, e.tz_id())),
248 ) else {
249 continue;
250 };
251 let (key, rid_utc) = match &master {
252 Some(m) => (m.key(&rid), m.utc_of(&rid)),
253 None => (rid.utc().timestamp(), rid.utc()),
254 };
255 if e.parameter(&ICalendarParameterName::Range).is_some() {
256 future.push((key, rid_utc, timing.clone()));
257 }
258 if exact.get(&key).is_none_or(|old| sequence(i) > old.sequence) {
259 let sequence = sequence(i);
260 exact.insert(
261 key,
262 Override {
263 sequence,
264 recurrence_id: rid_utc,
265 timing,
266 },
267 );
268 }
269 }
270 future.sort_by_key(|f| f.0);
271
272 let Some(m) = master else {
273 push_overrides(out, window, exact.values());
274 return;
275 };
276 let mc = comp(m.component);
277 let mz = &m.start.zone;
278
279 let rules: Vec<_> = mc
280 .properties(&ICalendarProperty::Rrule)
281 .filter_map(|e| rule(e.values.first()?))
282 .collect();
283 let mut set: BTreeMap<i64, Member> = BTreeMap::new();
284 let mut slack = m.length.max();
285 // DTSTART is an instance even when it does not match the RRULE, unless
286 // the series ended before it.
287 let ended = !rules.is_empty()
288 && rules.iter().all(|r| {
289 r.until
290 .as_ref()
291 .and_then(|u| until_local(u, &m.start))
292 .is_some_and(|u| u < m.start.local)
293 });
294 if !ended {
295 let utc = m.start.utc();
296 set.insert(
297 m.member_key(m.start.local, utc),
298 Member {
299 local: m.start.local,
300 utc,
301 length: None,
302 },
303 );
304 }
305 for e in mc.properties(&ICalendarProperty::Rdate) {
306 for v in &e.values {
307 let (s, length) = match v {
308 ICalendarValue::PartialDateTime(p) => {
309 let Some(s) = stamp(zones, p, e.tz_id()) else {
310 continue;
311 };
312 (s, None)
313 }
314 ICalendarValue::Period(ICalendarPeriod::Range { start, end }) => {
315 let (Some(s), Some(end)) =
316 (stamp(zones, start, e.tz_id()), stamp(zones, end, e.tz_id()))
317 else {
318 continue;
319 };
320 let d = (end.utc() - s.utc()).max(TimeDelta::zero());
321 (s, Some(Length::Exact(d)))
322 }
323 ICalendarValue::Period(ICalendarPeriod::Duration { start, duration }) => {
324 let Some(s) = stamp(zones, start, e.tz_id()) else {
325 continue;
326 };
327 (s, Some(nominal(duration)))
328 }
329 _ => continue,
330 };
331 let utc = m.utc_of(&s);
332 let local = mz.to_local(utc);
333 if let Some(l) = &length {
334 slack = slack.max(l.max());
335 }
336 set.entry(m.key(&s))
337 .or_insert(Member { local, utc, length });
338 }
339 }
340 for (_, rid, t) in &future {
341 slack = slack.max((t.start.utc() - *rid).abs() + t.length.max());
342 }
343 let slack = slack + TimeDelta::days(1);
344 let (from, to) = (add(window.start, -slack), add(window.end, slack));
345 let to_local = mz.to_local(to);
346
347 let mut excluded = HashSet::new();
348 for e in mc.properties(&ICalendarProperty::Exrule) {
349 let Some(rule) = e.values.first().and_then(rule) else {
350 continue;
351 };
352 for local in occurrences_capped(rule, &m, to_local, out) {
353 excluded.insert(m.member_key(local, mz.to_utc(local)));
354 }
355 }
356 for rule in rules {
357 for local in occurrences_capped(rule, &m, to_local, out) {
358 let utc = mz.to_utc(local);
359 if utc >= from {
360 set.entry(m.member_key(local, utc)).or_insert(Member {
361 local,
362 utc,
363 length: None,
364 });
365 }
366 }
367 }
368
369 // A DATE EXDATE on a DATE-TIME series removes every instance on that day.
370 let mut excluded_days = HashSet::new();
371 for e in mc.properties(&ICalendarProperty::Exdate) {
372 for s in e
373 .values
374 .iter()
375 .filter_map(|v| stamp(zones, v.as_partial_date_time()?, e.tz_id()))
376 {
377 if s.date && !m.start.date {
378 excluded_days.insert(s.local.date());
379 } else {
380 excluded.insert(m.key(&s));
381 }
382 }
383 }
384
385 // An EXDATE removes an override of that instance too. An override whose
386 // RECURRENCE-ID matches no instance still shows, as in clients.
387 push_overrides(
388 out,
389 window,
390 exact.iter().filter_map(|(key, o)| {
391 let day = mz.to_local(o.recurrence_id).date();
392 (!excluded.contains(key) && !excluded_days.contains(&day)).then_some(o)
393 }),
394 );
395
396 let recurs =
397 mc.has_property(&ICalendarProperty::Rrule) || mc.has_property(&ICalendarProperty::Rdate);
398 for (key, member) in &set {
399 if member.utc < from
400 || excluded.contains(key)
401 || excluded_days.contains(&member.local.date())
402 || exact.contains_key(key)
403 {
404 continue;
405 }
406 let recurrence_id = recurs.then_some(member.utc);
407 // The latest THISANDFUTURE override before this instance moves it by
408 // the same offset and gives it the override's length.
409 let instance = match future.iter().rev().find(|f| f.0 <= *key) {
410 Some((_, rid, t)) => {
411 let start = add(member.utc, t.start.utc() - *rid);
412 Instance {
413 start,
414 end: t.end_at(t.start.zone.to_local(start), start),
415 recurrence_id,
416 component: t.component,
417 }
418 }
419 None => Instance {
420 start: member.utc,
421 end: member.length.as_ref().map_or_else(
422 || m.end_at(member.local, member.utc),
423 |l| l.end(mz, member.local, member.utc),
424 ),
425 recurrence_id,
426 component: m.component,
427 },
428 };
429 push(out, window, instance);
430 }
431}
432
433fn push_overrides<'a>(
434 out: &mut Expansion,
435 window: &Range<DateTime<Utc>>,
436 overrides: impl Iterator<Item = &'a Override>,
437) {
438 for o in overrides {
439 let t = &o.timing;
440 let start = t.start.utc();
441 let end = t.end_at(t.start.local, start);
442 push(
443 out,
444 window,
445 Instance {
446 start,
447 end,
448 recurrence_id: Some(o.recurrence_id),
449 component: t.component,
450 },
451 );
452 }
453}
454
455fn occurrences_capped(
456 rule: &ICalendarRecurrenceRule,
457 m: &Timing,
458 to_local: NaiveDateTime,
459 out: &mut Expansion,
460) -> Vec<NaiveDateTime> {
461 let until = match &rule.until {
462 Some(u) => match until_local(u, &m.start) {
463 Some(u) => Some(u),
464 None => return Vec::new(),
465 },
466 None => None,
467 };
468 let mut list: Vec<_> = occurrences(rule, m.start.local, until, to_local)
469 .take(MAX_OCCURRENCES + 1)
470 .collect();
471 if list.len() > MAX_OCCURRENCES {
472 list.pop();
473 out.truncated = true;
474 }
475 list
476}
477
478fn push(out: &mut Expansion, window: &Range<DateTime<Utc>>, i: Instance) {
479 let overlaps = if i.start == i.end {
480 window.contains(&i.start)
481 } else {
482 i.start < window.end && i.end > window.start
483 };
484 if overlaps {
485 out.instances.push(i);
486 }
487}
488
489/// UNTIL as wall-clock time in the zone of DTSTART. The rule iterates in wall
490/// time, so a UTC UNTIL compared as-is cuts the last instance east of UTC.
491fn until_local(u: &PartialDateTime, start: &Stamp) -> Option<NaiveDateTime> {
492 let dt = u.to_date_time()?;
493 Some(match dt.offset {
494 Some(o) => {
495 let utc = dt.date_time - TimeDelta::seconds(o.local_minus_utc().into());
496 start.zone.to_local(utc.and_utc())
497 }
498 // A DATE on a DATE-TIME series includes that whole day.
499 None if u.hour.is_none() && !start.date => dt.date_time.date().and_hms_opt(23, 59, 59)?,
500 // Floating, or a DATE on an all-day series: already wall time.
501 None => dt.date_time,
502 })
503}
504
505fn day_key(local: NaiveDateTime) -> i64 {
506 local.date().and_time(NaiveTime::MIN).and_utc().timestamp()
507}
508
509fn nominal(d: &ICalendarDuration) -> Length {
510 if d.neg {
511 return Length::Exact(TimeDelta::zero());
512 }
513 Length::Nominal {
514 days: i64::from(d.weeks) * 7 + i64::from(d.days),
515 exact: TimeDelta::seconds(
516 i64::from(d.hours) * 3600 + i64::from(d.minutes) * 60 + i64::from(d.seconds),
517 ),
518 }
519}
520
521fn stamp(zones: &Zones, v: &PartialDateTime, tzid: Option<&str>) -> Option<Stamp> {
522 let dt = v.to_date_time()?;
523 let date = v.hour.is_none();
524 let zone = match dt.offset {
525 Some(o) if o.local_minus_utc() == 0 => Zone::Utc,
526 Some(o) => Zone::Fixed(o.local_minus_utc()),
527 None if date => zones.floating().clone(),
528 None => zones.get(tzid),
529 };
530 Some(Stamp {
531 local: dt.date_time,
532 zone,
533 date,
534 floating: !date && dt.offset.is_none() && tzid.is_none(),
535 })
536}
537
538fn prop_stamp(zones: &Zones, c: &ICalendarComponent, prop: &ICalendarProperty) -> Option<Stamp> {
539 let e = c.property(prop)?;
540 stamp(zones, e.values.first()?.as_partial_date_time()?, e.tz_id())
541}
542
543pub(crate) fn rule(v: &ICalendarValue) -> Option<&ICalendarRecurrenceRule> {
544 match v {
545 ICalendarValue::RecurrenceRule(r) => Some(r),
546 _ => None,
547 }
548}
549
550/// Wall-clock occurrences of `rule` from `start` up to `end`. An invalid or
551/// unsupported rule yields none.
552pub(crate) fn occurrences(
553 rule: &ICalendarRecurrenceRule,
554 start: NaiveDateTime,
555 until: Option<NaiveDateTime>,
556 end: NaiveDateTime,
557) -> impl Iterator<Item = NaiveDateTime> {
558 let wall = |t: NaiveDateTime| rrule::Tz::UTC.from_utc_datetime(&t);
559 let freq = match rule.freq {
560 ICalendarFrequency::Yearly => rrule::Frequency::Yearly,
561 ICalendarFrequency::Monthly => rrule::Frequency::Monthly,
562 ICalendarFrequency::Weekly => rrule::Frequency::Weekly,
563 ICalendarFrequency::Daily => rrule::Frequency::Daily,
564 ICalendarFrequency::Hourly => rrule::Frequency::Hourly,
565 ICalendarFrequency::Minutely => rrule::Frequency::Minutely,
566 ICalendarFrequency::Secondly => rrule::Frequency::Secondly,
567 };
568 // Other calendar scales and leap months (RFC 7529) are unsupported. RFC
569 // 7529 allows treating such a rule as absent.
570 let months: Option<Vec<chrono::Month>> = rule
571 .bymonth
572 .iter()
573 .map(|m| (!m.is_leap()).then(|| chrono::Month::try_from(m.month()).ok())?)
574 .collect();
575 let valid = months.is_some()
576 && rule
577 .rscale
578 .as_ref()
579 .is_none_or(|s| *s == CalendarScale::Gregorian)
580 && rule.skip.is_none_or(|s| s == ICalendarSkip::Omit);
581 // rrule intersects plain and numbered weekdays, RFC 5545 unites them.
582 // Numbering every occurrence of the plain ones gives the union.
583 let mixed = rule.byday.iter().any(|d| d.ordwk.is_some())
584 && rule.byday.iter().any(|d| d.ordwk.is_none());
585 let max_nth = if rule.freq == ICalendarFrequency::Monthly || !rule.bymonth.is_empty() {
586 5
587 } else {
588 53
589 };
590 let weekdays = rule
591 .byday
592 .iter()
593 .flat_map(|d| {
594 let wd = d.weekday.into();
595 match d.ordwk {
596 Some(n) => vec![rrule::NWeekday::Nth(n, wd)],
597 None if mixed => (1..=max_nth).map(|n| rrule::NWeekday::Nth(n, wd)).collect(),
598 None => vec![rrule::NWeekday::Every(wd)],
599 }
600 })
601 .collect();
602 let mut r = rrule::RRule::new(freq)
603 .interval(rule.interval.unwrap_or(1).max(1))
604 .week_start(rule.wkst.map_or(Weekday::Mon, Into::into))
605 .by_set_pos(rule.bysetpos.clone())
606 .by_month(&months.unwrap_or_default())
607 .by_month_day(rule.bymonthday.clone())
608 .by_year_day(rule.byyearday.clone())
609 .by_week_no(rule.byweekno.clone())
610 .by_weekday(weekdays)
611 .by_hour(rule.byhour.clone())
612 .by_minute(rule.byminute.clone())
613 .by_second(rule.bysecond.clone());
614 // COUNT and UNTIL together are invalid. UNTIL is the safer bound.
615 if let Some(n) = rule.count.filter(|_| until.is_none()) {
616 r = r.count(n);
617 }
618 if let Some(u) = until {
619 r = r.until(wall(u));
620 }
621 r.build(wall(start))
622 .ok()
623 .filter(|_| valid)
624 .map(|set| (&set).into_iter())
625 .into_iter()
626 .flatten()
627 .map(|t| t.naive_utc())
628 .take_while(move |t| *t <= end)
629}
630