|
1 | 1 | use std::{str::FromStr as _, time::Duration, vec}; |
2 | 2 |
|
3 | | -use chrono::{DateTime, Local, Utc}; |
4 | | -use icalendar::{Component as _, EventLike as _}; |
| 3 | +use chrono::{DateTime, Datelike as _, Local, TimeZone as _, Timelike as _, Utc}; |
| 4 | +use icalendar::{Component as _, EventLike as _, rrule::Tz}; |
5 | 5 | use reqwest::{ |
6 | 6 | self, ClientBuilder, Method, Url, |
7 | 7 | header::{CONTENT_TYPE, HeaderMap, HeaderValue}, |
@@ -301,20 +301,57 @@ fn parse_events(multi_status: Multistatus) -> Result<Vec<Event>, CalendarError> |
301 | 301 | icalendar::Calendar::from_str(&data).map_err(CalendarError::Parsing)?; |
302 | 302 | for component in calendar.components { |
303 | 303 | if let icalendar::CalendarComponent::Event(event) = component { |
304 | | - let start_at = event.get_start().and_then(|d| match d { |
| 304 | + let mut start_at = event.get_start().and_then(|d| match d { |
305 | 305 | icalendar::DatePerhapsTime::DateTime(dt) => dt.try_into_utc(), |
306 | 306 | icalendar::DatePerhapsTime::Date(d) => d |
307 | 307 | .and_hms_opt(0, 0, 0) |
308 | 308 | .and_then(|d| d.and_local_timezone(Local).earliest()) |
309 | 309 | .map(|d| d.to_utc()), |
310 | 310 | }); |
311 | | - let end_at = event.get_end().and_then(|d| match d { |
| 311 | + let mut end_at = event.get_end().and_then(|d| match d { |
312 | 312 | icalendar::DatePerhapsTime::DateTime(dt) => dt.try_into_utc(), |
313 | 313 | icalendar::DatePerhapsTime::Date(d) => d |
314 | 314 | .and_hms_opt(23, 59, 59) |
315 | 315 | .and_then(|d| d.and_local_timezone(Local).earliest()) |
316 | 316 | .map(|d| d.to_utc()), |
317 | 317 | }); |
| 318 | + |
| 319 | + let today_dt = Local::now() |
| 320 | + .date_naive() |
| 321 | + .and_hms_opt(0, 0, 0) |
| 322 | + .expect("A valid time") |
| 323 | + .and_local_timezone(Local) |
| 324 | + .earliest() |
| 325 | + .expect("A valid datetime") |
| 326 | + .to_utc(); |
| 327 | + let today_rrule_dt = Tz::UTC |
| 328 | + .with_ymd_and_hms( |
| 329 | + today_dt.year(), |
| 330 | + today_dt.month(), |
| 331 | + today_dt.day(), |
| 332 | + today_dt.hour(), |
| 333 | + today_dt.minute(), |
| 334 | + today_dt.second(), |
| 335 | + ) |
| 336 | + .earliest() |
| 337 | + .unwrap(); |
| 338 | + |
| 339 | + if let Some(s) = start_at |
| 340 | + && let Some(rrule_set) = event.get_recurrence() |
| 341 | + && let Some(new_start) = rrule_set |
| 342 | + .after(today_rrule_dt) |
| 343 | + .all(1) |
| 344 | + .dates |
| 345 | + .first() |
| 346 | + .map(|dt| dt.to_utc()) |
| 347 | + { |
| 348 | + start_at.replace(new_start); |
| 349 | + if let Some(e) = end_at { |
| 350 | + let duration = e - s; |
| 351 | + end_at.replace(new_start + duration); |
| 352 | + } |
| 353 | + } |
| 354 | + |
318 | 355 | result.push(Event { |
319 | 356 | uid: event.get_uid().map(Into::into), |
320 | 357 | summary: event.get_summary().map(Into::into), |
|
0 commit comments