fix: Changes EventScheduler API so it is more explicit (#2164)
### Summary
Refactors ScheduledEvent and EventScheduler API to use TimeOnly so recurrence offset is explicit.
API:
```cs
public ScheduledEvent(
DateTime startAfter,
DateTime endOn,
TimeOnly time,
IRecurrencePattern recurrence,
TimeZoneInfo timeZone = null
)
```
Example:
```cs
// Schedule a daily event at 8:00 AM UTC
EventScheduler.DailyAt(
new DateTime(2024, 6, 1, 8, 0, 0, DateTimeKind.Utc),
() => Console.WriteLine("Daily event triggered!")
);
// Schedule a custom recurring event at 3:30 PM UTC every Monday
var recurrence = new WeeklyRecurrencePattern(1, DaysOfWeek.Monday);
EventScheduler.Shared.ScheduleEvent(
DateTime.UtcNow,
new TimeOnly(15, 30),
() => Console.WriteLine("Weekly Monday event!"),
recurrence
);
```
This commit is contained in:
parent
f89b2be653
commit
21a4092dd8
7 changed files with 495 additions and 250 deletions
|
|
@ -8,7 +8,13 @@ public class HourlyRecurrencePattern : IRecurrencePattern
|
|||
|
||||
public HourlyRecurrencePattern(int intervalHours = 1) => IntervalHours = Math.Max(1, intervalHours);
|
||||
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeZoneInfo timeZone) => afterUtc.AddHours(IntervalHours);
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeOnly time, TimeZoneInfo timeZone)
|
||||
{
|
||||
var local = TimeZoneInfo.ConvertTimeFromUtc(afterUtc, timeZone);
|
||||
return new DateTime(local.Year, local.Month, local.Day, local.Hour, time.Minute, 0)
|
||||
.LocalToUtc(timeZone)
|
||||
.AddHours(IntervalHours);
|
||||
}
|
||||
}
|
||||
|
||||
public class DailyRecurrencePattern : IRecurrencePattern
|
||||
|
|
@ -17,7 +23,13 @@ public class DailyRecurrencePattern : IRecurrencePattern
|
|||
|
||||
public DailyRecurrencePattern(int intervalDays = 1) => IntervalDays = Math.Max(1, intervalDays);
|
||||
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeZoneInfo timeZone) => afterUtc.AddDays(IntervalDays);
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeOnly time, TimeZoneInfo timeZone)
|
||||
{
|
||||
var local = TimeZoneInfo.ConvertTimeFromUtc(afterUtc, timeZone);
|
||||
return new DateTime(local.Year, local.Month, local.Day, time.Hour, time.Minute, 0)
|
||||
.LocalToUtc(timeZone)
|
||||
.AddDays(IntervalDays);
|
||||
}
|
||||
}
|
||||
|
||||
public class WeeklyRecurrencePattern : IRecurrencePattern
|
||||
|
|
@ -31,10 +43,12 @@ public class WeeklyRecurrencePattern : IRecurrencePattern
|
|||
DaysOfWeek = daysOfWeek;
|
||||
}
|
||||
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeZoneInfo timeZone)
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeOnly time, TimeZoneInfo timeZone)
|
||||
{
|
||||
var local = TimeZoneInfo.ConvertTimeFromUtc(afterUtc, timeZone);
|
||||
var weekStart = local.Date.AddDays(1);
|
||||
var localDate = DateOnly.FromDateTime(local);
|
||||
|
||||
var weekStart = localDate.ToDateTime(time);
|
||||
var daysOfWeek = DaysOfWeek;
|
||||
|
||||
// Set the day of the week to whatever day it is now
|
||||
|
|
@ -46,7 +60,7 @@ public class WeeklyRecurrencePattern : IRecurrencePattern
|
|||
// Example:
|
||||
// Recurrence is Monday, Wednesday, Friday - and today is Wednesday
|
||||
// weekStart will be Thursday, and then we check every day for 7 days to find the next occurrence match.
|
||||
for (int i = 0; i < 7; i++)
|
||||
for (int i = 1; i < 8; i++)
|
||||
{
|
||||
var candidate = weekStart.AddDays(i);
|
||||
var candidateDay = (DaysOfWeek)(1 << (int)candidate.DayOfWeek);
|
||||
|
|
@ -72,12 +86,11 @@ public class MonthlyRecurrencePattern : IRecurrencePattern
|
|||
IntervalMonths = Math.Max(1, intervalMonths);
|
||||
}
|
||||
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeZoneInfo timeZone)
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeOnly time, TimeZoneInfo timeZone)
|
||||
{
|
||||
var local = TimeZoneInfo.ConvertTimeFromUtc(afterUtc, timeZone);
|
||||
var year = local.Year;
|
||||
var month = local.Month;
|
||||
var time = local.TimeOfDay;
|
||||
var day = DayOfMonth == -1 ? local.Day : DayOfMonth;
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
|
|
@ -86,7 +99,7 @@ public class MonthlyRecurrencePattern : IRecurrencePattern
|
|||
var candidate = new DateTime(year, 1, 1)
|
||||
.AddMonths(nextMonth - 1)
|
||||
.AddDays(day - 1)
|
||||
.Add(time);
|
||||
.Add(time.ToTimeSpan());
|
||||
|
||||
// Some months may not have that day of the month, if not, we skip to the next interval
|
||||
if (candidate > local && candidate.Day == day && !timeZone.IsInvalidTime(candidate))
|
||||
|
|
@ -112,7 +125,7 @@ public class MonthlyOrdinalRecurrencePattern : IRecurrencePattern
|
|||
DayOfWeek = dayOfWeek;
|
||||
}
|
||||
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeZoneInfo timeZone)
|
||||
public DateTime GetNextOccurrence(DateTime afterUtc, TimeOnly time, TimeZoneInfo timeZone)
|
||||
{
|
||||
var local = TimeZoneInfo.ConvertTimeFromUtc(afterUtc, timeZone);
|
||||
var year = local.Year;
|
||||
|
|
@ -129,7 +142,8 @@ public class MonthlyOrdinalRecurrencePattern : IRecurrencePattern
|
|||
if (Ordinal >= OrdinalDayOccurrence.First)
|
||||
{
|
||||
// Find the first day of the month
|
||||
var firstOfMonth = new DateTime(candidateYear, candidateMonth, 1, local.Hour, local.Minute, local.Second);
|
||||
var firstOfMonth = new DateTime(candidateYear, candidateMonth, 1)
|
||||
.Add(time.ToTimeSpan());
|
||||
|
||||
// Find the first occurrence of the desired day
|
||||
int daysOffset = ((int)DayOfWeek - (int)firstOfMonth.DayOfWeek + 7) % 7;
|
||||
|
|
@ -149,7 +163,8 @@ public class MonthlyOrdinalRecurrencePattern : IRecurrencePattern
|
|||
{
|
||||
// Find the last day of the month
|
||||
var daysInMonth = DateTime.DaysInMonth(candidateYear, candidateMonth);
|
||||
var lastOfMonth = new DateTime(candidateYear, candidateMonth, daysInMonth, local.Hour, local.Minute, local.Second);
|
||||
var lastOfMonth = new DateTime(candidateYear, candidateMonth, daysInMonth)
|
||||
.Add(time.ToTimeSpan());
|
||||
|
||||
// Find the last occurrence of the desired day
|
||||
int daysOffset = (int)lastOfMonth.DayOfWeek - (int)DayOfWeek + 7;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue