Fix google calendar time zone
This commit is contained in:
parent
28429d78d0
commit
a2fa0c11cb
3 changed files with 22 additions and 14 deletions
7
.github/copilot-instructions.md
vendored
7
.github/copilot-instructions.md
vendored
|
|
@ -170,6 +170,13 @@ void Foo(params IReadOnlyList<string> values) => // actual implementation here.`
|
||||||
- Use proper null checks in equality methods
|
- Use proper null checks in equality methods
|
||||||
- Consider `ReferenceEquals` optimization for reference types
|
- 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
|
### Architecture
|
||||||
- Proper separation of concerns
|
- Proper separation of concerns
|
||||||
- Following established patterns in the codebase
|
- Following established patterns in the codebase
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@ namespace ShiftScheduler.Services
|
||||||
{
|
{
|
||||||
public interface IConfigurationService
|
public interface IConfigurationService
|
||||||
{
|
{
|
||||||
string GetTimeZone();
|
(DateTimeOffset StartTime, DateTimeOffset EndTime) GetZurichTime(DateOnly date, string timeRange);
|
||||||
ApplicationConfiguration GetConfiguration();
|
ApplicationConfiguration GetConfiguration();
|
||||||
void UpdateConfiguration(ApplicationConfiguration configuration);
|
void UpdateConfiguration(ApplicationConfiguration configuration);
|
||||||
Task<string> ExportConfigurationAsync();
|
Task<string> ExportConfigurationAsync();
|
||||||
|
|
@ -32,9 +32,14 @@ namespace ShiftScheduler.Services
|
||||||
|
|
||||||
private readonly TimeZoneInfo localTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Europe/Zurich");
|
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)
|
public ConfigurationService(ApplicationConfiguration initialConfiguration)
|
||||||
|
|
|
||||||
|
|
@ -101,24 +101,22 @@ public class GoogleCalendarService(IHttpContextAccessor httpContextAccessor, ICo
|
||||||
// Create morning event if it exists
|
// Create morning event if it exists
|
||||||
if (!string.IsNullOrEmpty(shift.MorningTime))
|
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());
|
await ExecuteWithRetryAsync(async () => await service.Events.Insert(morningEvent, calendarId).ExecuteAsync());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create afternoon event if it exists
|
// Create afternoon event if it exists
|
||||||
if (!string.IsNullOrEmpty(shift.AfternoonTime))
|
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());
|
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 summary = $"{shift.Name}";
|
||||||
var description = "";
|
var description = "";
|
||||||
|
|
||||||
|
|
@ -134,13 +132,11 @@ public class GoogleCalendarService(IHttpContextAccessor httpContextAccessor, ICo
|
||||||
Description = description,
|
Description = description,
|
||||||
Start = new EventDateTime
|
Start = new EventDateTime
|
||||||
{
|
{
|
||||||
DateTimeDateTimeOffset = startTime,
|
DateTimeDateTimeOffset = startTime
|
||||||
TimeZone = configurationService.GetTimeZone()
|
|
||||||
},
|
},
|
||||||
End = new EventDateTime
|
End = new EventDateTime
|
||||||
{
|
{
|
||||||
DateTimeDateTimeOffset = endTime,
|
DateTimeDateTimeOffset = endTime
|
||||||
TimeZone = configurationService.GetTimeZone()
|
|
||||||
},
|
},
|
||||||
ExtendedProperties = new Event.ExtendedPropertiesData
|
ExtendedProperties = new Event.ExtendedPropertiesData
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue