Fix google calendar time zone

This commit is contained in:
Claudio Schaad 2025-09-06 21:37:59 +02:00
parent 28429d78d0
commit a2fa0c11cb
3 changed files with 22 additions and 14 deletions

View file

@ -170,6 +170,13 @@ void Foo(params IReadOnlyList<string> values) => // actual implementation here.`
- Use proper null checks in equality methods
- Consider `ReferenceEquals` optimization for reference types
### Date, Time and TimeZone
- Don't use DateTime for a date with time, use DateTimeOffset instead
- When only a date is needed, use DateOnly
- When only a time is needed, use TimeOnly
- Always use a specific local timezone for the DateTimeOffset
- If nothing specified, use Europe/Zurich
### Architecture
- Proper separation of concerns
- Following established patterns in the codebase

View file

@ -5,7 +5,7 @@ namespace ShiftScheduler.Services
{
public interface IConfigurationService
{
string GetTimeZone();
(DateTimeOffset StartTime, DateTimeOffset EndTime) GetZurichTime(DateOnly date, string timeRange);
ApplicationConfiguration GetConfiguration();
void UpdateConfiguration(ApplicationConfiguration configuration);
Task<string> ExportConfigurationAsync();
@ -32,9 +32,14 @@ namespace ShiftScheduler.Services
private readonly TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Zurich");
public string GetTimeZone()
public (DateTimeOffset StartTime, DateTimeOffset EndTime) GetZurichTime(DateOnly date, string timeRange)
{
return localTimeZone.Id;
var times = timeRange.Split('-'); ;
var startTime = TimeOnly.Parse(times[0]);
var endTime = TimeOnly.Parse(times[1]);
TimeSpan offset = localTimeZone.GetUtcOffset(DateTime.UtcNow);
return (new DateTimeOffset(date, startTime, offset), new DateTimeOffset(date, endTime, offset));
}
public ConfigurationService(ApplicationConfiguration initialConfiguration)

View file

@ -101,24 +101,22 @@ public class GoogleCalendarService(IHttpContextAccessor httpContextAccessor, ICo
// Create morning event if it exists
if (!string.IsNullOrEmpty(shift.MorningTime))
{
var morningEvent = CreateEventFromShift(shift, date, shift.MorningTime, shiftWithTransport.MorningTransport);
var (startTime, endTime) = configurationService.GetZurichTime(DateOnly.FromDateTime(date), shift.MorningTime);
var morningEvent = CreateEventFromShift(shift, startTime, endTime, shiftWithTransport.MorningTransport);
await ExecuteWithRetryAsync(async () => await service.Events.Insert(morningEvent, calendarId).ExecuteAsync());
}
// Create afternoon event if it exists
if (!string.IsNullOrEmpty(shift.AfternoonTime))
{
var afternoonEvent = CreateEventFromShift(shift, date, shift.AfternoonTime, shiftWithTransport.AfternoonTransport);
var (startTime, endTime) = configurationService.GetZurichTime(DateOnly.FromDateTime(date), shift.AfternoonTime);
var afternoonEvent = CreateEventFromShift(shift, startTime, endTime, shiftWithTransport.AfternoonTransport);
await ExecuteWithRetryAsync(async () => await service.Events.Insert(afternoonEvent, calendarId).ExecuteAsync());
}
}
private Event CreateEventFromShift(Shift shift, DateTime date, string timeRange, TransportConnection? transport)
private Event CreateEventFromShift(Shift shift, DateTimeOffset startTime, DateTimeOffset endTime, TransportConnection? transport)
{
var times = timeRange.Split('-');
var startTime = date.Add(TimeSpan.Parse(times[0]));
var endTime = date.Add(TimeSpan.Parse(times[1]));
var summary = $"{shift.Name}";
var description = "";
@ -134,13 +132,11 @@ public class GoogleCalendarService(IHttpContextAccessor httpContextAccessor, ICo
Description = description,
Start = new EventDateTime
{
DateTimeDateTimeOffset = startTime,
TimeZone = configurationService.GetTimeZone()
DateTimeDateTimeOffset = startTime
},
End = new EventDateTime
{
DateTimeDateTimeOffset = endTime,
TimeZone = configurationService.GetTimeZone()
DateTimeDateTimeOffset = endTime
},
ExtendedProperties = new Event.ExtendedPropertiesData
{