Fix Google Calendar UTC datetime format parsing

Google Calendar ICS files use UTC datetime format ending with 'Z' (e.g., 20250817T140000Z)
which was failing to parse with DateTime::parse_from_str. Fixed by detecting 'Z' suffix,
parsing as naive datetime, and converting to UTC with and_utc().

Also cleaned up debug logging and unused variable warnings.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Connor Johnstone
2025-09-03 22:28:51 -04:00
parent 75d9149c76
commit 64c737c023

View File

@@ -96,14 +96,12 @@ pub async fn fetch_external_calendar_events(
// Store in cache for future requests // Store in cache for future requests
let etag = None; // TODO: Extract ETag from response headers if available let etag = None; // TODO: Extract ETag from response headers if available
if let Err(e) = repo.update_cache(id, &ics_content, etag).await { if let Err(_) = repo.update_cache(id, &ics_content, etag).await {
// Log error but don't fail the request // Log error but don't fail the request
eprintln!("Failed to update cache for calendar {}: {}", id, e);
} }
// Update last_fetched timestamp // Update last_fetched timestamp
if let Err(e) = repo.update_last_fetched(id, &user.id).await { if let Err(_) = repo.update_last_fetched(id, &user.id).await {
eprintln!("Failed to update last_fetched for calendar {}: {}", id, e);
} }
last_fetched = Utc::now(); last_fetched = Utc::now();
@@ -122,16 +120,25 @@ pub async fn fetch_external_calendar_events(
fn parse_ics_content(ics_content: &str) -> Result<Vec<VEvent>, Box<dyn std::error::Error>> { fn parse_ics_content(ics_content: &str) -> Result<Vec<VEvent>, Box<dyn std::error::Error>> {
let reader = ical::IcalParser::new(ics_content.as_bytes()); let reader = ical::IcalParser::new(ics_content.as_bytes());
let mut events = Vec::new(); let mut events = Vec::new();
let mut _total_components = 0;
let mut _failed_conversions = 0;
for calendar in reader { for calendar in reader {
let calendar = calendar?; let calendar = calendar?;
for component in calendar.events { for component in calendar.events {
if let Ok(vevent) = convert_ical_to_vevent(component) { _total_components += 1;
events.push(vevent); match convert_ical_to_vevent(component) {
Ok(vevent) => {
events.push(vevent);
}
Err(_) => {
_failed_conversions += 1;
}
} }
} }
} }
Ok(events) Ok(events)
} }
@@ -147,6 +154,7 @@ fn convert_ical_to_vevent(ical_event: IcalEvent) -> Result<VEvent, Box<dyn std::
let mut all_day = false; let mut all_day = false;
let mut rrule = None; let mut rrule = None;
// Extract properties // Extract properties
for property in ical_event.properties { for property in ical_event.properties {
match property.name.as_str() { match property.name.as_str() {
@@ -211,9 +219,12 @@ fn convert_ical_to_vevent(ical_event: IcalEvent) -> Result<VEvent, Box<dyn std::
} }
} }
let dtstart = dtstart.ok_or("Missing DTSTART")?;
let vevent = VEvent { let vevent = VEvent {
uid: uid.unwrap_or_else(|| Uuid::new_v4().to_string()), uid: uid.unwrap_or_else(|| Uuid::new_v4().to_string()),
dtstart: dtstart.ok_or("Missing DTSTART")?, dtstart,
dtend, dtend,
summary, summary,
description, description,
@@ -255,11 +266,15 @@ fn parse_datetime_with_tz(datetime_str: &str, tzid: Option<&str>) -> Option<Date
use chrono::TimeZone; use chrono::TimeZone;
use chrono_tz::Tz; use chrono_tz::Tz;
// Try various datetime formats commonly found in ICS files // Try various datetime formats commonly found in ICS files
// Format: 20231201T103000Z (UTC) // Format: 20231201T103000Z (UTC) - handle as naive datetime first
if let Ok(dt) = DateTime::parse_from_str(datetime_str, "%Y%m%dT%H%M%SZ") { if datetime_str.ends_with('Z') {
return Some(dt.with_timezone(&Utc)); let datetime_without_z = &datetime_str[..datetime_str.len()-1];
if let Ok(naive_dt) = chrono::NaiveDateTime::parse_from_str(datetime_without_z, "%Y%m%dT%H%M%S") {
return Some(naive_dt.and_utc());
}
} }
// Format: 20231201T103000-0500 (with timezone offset) // Format: 20231201T103000-0500 (with timezone offset)