Proper handling of DateTimeOffset, support for Multi-Day events and reacting to adding/removing events for the days.

This commit is contained in:
Burak Kaan Köse
2024-12-30 23:10:51 +01:00
parent 8cc7d46d7b
commit 8fd09bcad4
23 changed files with 340 additions and 234 deletions

View File

@@ -209,88 +209,74 @@ namespace Wino.Core.Extensions
/// </summary>
/// <param name="calendarEvent">The Google Calendar Event object.</param>
/// <returns>The start DateTimeOffset of the event, or null if it cannot be determined.</returns>
public static DateTimeOffset? GetEventStartDateTimeOffset(this Event calendarEvent)
{
if (calendarEvent == null)
{
return null;
}
//public static DateTimeOffset GetEventStartDateTimeOffset(this Event calendarEvent)
//{
// return GetEventDateTimeOffset(calendarEvent.Start);
//}
if (calendarEvent.Start != null)
public static DateTimeOffset GetEventDateTimeOffset(EventDateTime calendarEvent)
{
if (calendarEvent != null)
{
if (calendarEvent.Start.DateTimeDateTimeOffset != null)
if (calendarEvent.DateTimeDateTimeOffset != null)
{
return calendarEvent.Start.DateTimeDateTimeOffset; // Use the direct DateTimeOffset property!
return calendarEvent.DateTimeDateTimeOffset.Value;
}
else if (calendarEvent.Start.Date != null)
else if (calendarEvent.Date != null)
{
if (DateTime.TryParse(calendarEvent.Start.Date, out DateTime startDate))
if (DateTime.TryParse(calendarEvent.Date, out DateTime eventDateTime))
{
// Date-only events are treated as UTC midnight
return new DateTimeOffset(startDate, TimeSpan.Zero);
return new DateTimeOffset(eventDateTime, TimeSpan.Zero);
}
else
{
return null;
throw new Exception("Invalid date format in Google Calendar event date.");
}
}
}
return null; // Start time not found
throw new Exception("Google Calendar event has no date.");
}
/// <summary>
/// Calculates the duration of a Google Calendar Event in minutes.
/// Calculates the duration of a Google Calendar Event in seconds.
/// Handles date-only and date-time events, but *does not* handle recurring events correctly.
/// For recurring events, this method will return the duration of the *first* instance.
/// </summary>
/// <param name="calendarEvent">The Google Calendar Event object.</param>
/// <returns>The duration of the event in minutes, or null if it cannot be determined.</returns>
public static int? GetEventDurationInMinutes(this Event calendarEvent)
{
if (calendarEvent == null)
{
return null;
}
//public static int GetEventDurationInSeconds(this Event calendarEvent)
//{
// var start = calendarEvent.GetEventStartDateTimeOffset();
DateTimeOffset? start = calendarEvent.GetEventStartDateTimeOffset();
if (start == null)
{
return null;
}
// DateTimeOffset? end = null;
// if (calendarEvent.End != null)
// {
// if (calendarEvent.End.DateTimeDateTimeOffset != null)
// {
// end = calendarEvent.End.DateTimeDateTimeOffset;
// }
// else if (calendarEvent.End.Date != null)
// {
// if (DateTime.TryParse(calendarEvent.End.Date, out DateTime endDate))
// {
// end = new DateTimeOffset(endDate, TimeSpan.Zero);
// }
// else
// {
// throw new Exception("Invalid date format in Google Calendar event end date.");
// }
// }
// }
DateTimeOffset? end = null;
if (calendarEvent.End != null)
{
if (calendarEvent.End.DateTimeDateTimeOffset != null)
{
end = calendarEvent.End.DateTimeDateTimeOffset;
}
else if (calendarEvent.End.Date != null)
{
if (DateTime.TryParse(calendarEvent.End.Date, out DateTime endDate))
{
end = new DateTimeOffset(endDate, TimeSpan.Zero);
}
else
{
return null;
}
}
}
if (end == null)
{
return null;
}
return (int)(end.Value - start.Value).TotalMinutes;
}
// if (end == null) throw new Exception("Google Calendar event has no end date.");
// return (int)(end.Value - start).TotalSeconds;
//}
/// <summary>
/// RRULE, EXRULE, RDATE and EXDATE lines for a recurring event, as specified in RFC5545.
///
/// </summary>
/// <returns>___ separated lines.</returns>
public static string GetRecurrenceString(this Event calendarEvent)

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Google.Apis.Calendar.v3.Data;
using Wino.Core.Domain.Entities.Calendar;
@@ -33,22 +34,32 @@ namespace Wino.Core.Integration.Processors
public async Task<CalendarItem> CreateCalendarItemAsync(Event calendarEvent, AccountCalendar assignedCalendar, MailAccount organizerAccount)
{
var eventStartDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.Start);
var eventEndDateTimeOffset = GoogleIntegratorExtensions.GetEventDateTimeOffset(calendarEvent.End);
var totalDurationInSeconds = (eventEndDateTimeOffset - eventStartDateTimeOffset).TotalSeconds;
var calendarItem = new CalendarItem()
{
CalendarId = assignedCalendar.Id,
CreatedAt = DateTimeOffset.UtcNow,
Description = calendarEvent.Description,
StartTime = GoogleIntegratorExtensions.GetEventStartDateTimeOffset(calendarEvent) ?? throw new Exception("Event without a start time."),
DurationInMinutes = GoogleIntegratorExtensions.GetEventDurationInMinutes(calendarEvent) ?? throw new Exception("Event without a duration."),
Id = Guid.NewGuid(),
StartDate = eventStartDateTimeOffset.DateTime,
StartDateOffset = eventStartDateTimeOffset.Offset,
EndDateOffset = eventEndDateTimeOffset.Offset,
DurationInSeconds = totalDurationInSeconds,
Location = calendarEvent.Location,
Recurrence = GoogleIntegratorExtensions.GetRecurrenceString(calendarEvent),
Status = GetStatus(calendarEvent.Status),
Title = calendarEvent.Summary,
UpdatedAt = DateTimeOffset.UtcNow,
Visibility = GetVisibility(calendarEvent.Visibility),
HtmlLink = calendarEvent.HtmlLink
};
Debug.WriteLine($"({assignedCalendar.Name}) {calendarItem.Title}, Start: {calendarItem.StartDate.ToString("f")}, End: {calendarItem.EndDate.ToString("f")}");
// TODO: There are some edge cases with cancellation here.
CalendarItemStatus GetStatus(string status)
{