Skip to content

Commit

Permalink
Resolved issue with .FirstOrDefault which was returning DateTime.MinV…
Browse files Browse the repository at this point in the history
…alue if no schedule was found.

Resolved issue where a schedule was not correctly calculated if the daylight saving offset occurred on the next iteration of the schedule.
  • Loading branch information
CraigHawker committed Oct 23, 2024
1 parent 5b3d33a commit 843fd90
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,5 +193,23 @@ public static IEnumerable<object[]> GetNextDayOfWeekData()
new DateTime ?[] { new DateTime(2021, 03, 20, 0, 0, 0, 0) }
};
}

[TestMethod]
public void WeeklyValueNotReturnedWhenDaylightsavingChanges()
{
var now = new DateTimeOffset(new DateTime(2024, 10, 23, 10, 27, 0), new TimeSpan(0, 0, 0));
var expected = new DateTimeOffset(new DateTime(2024, 10, 30, 9, 0, 0), new TimeSpan(2, 0, 0));

var trigger = new WeeklyTrigger()
{
TriggerTimes = new List<TimeSpan>()
{
new TimeSpan(9, 0, 0)
},
TriggerDays = new List<DayOfWeek>() { DayOfWeek.Wednesday }
};
var execution = trigger.GetNextExecution(now, TimeZoneInfo.FindSystemTimeZoneById("FLE Standard Time"));
Assert.AreEqual(expected.ToUniversalTime(), execution?.ToUniversalTime());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public DailyTrigger()
timeZoneInfo = timeZoneInfo ?? TimeZoneInfo.Local;

// When should we start looking?
var before = after.Value;
after = (after ?? DateTime.UtcNow).ToUniversalTime();

// Convert the time into the timezone we're after.
Expand All @@ -73,7 +74,12 @@ public DailyTrigger()
// What is the potential time that this will run?
DateTimeOffset potential;
{
var dateTime = after.Value.Date.Add(t);
// If the timezone conversion changed the date then go back to the start of the date.
var date = after.Value.Date;
if (after.Value.Date != before.Date)
date = new DateTime(before.Date.Ticks);

var dateTime = date.Add(t);
potential = new DateTimeOffset(dateTime, timeZoneInfo.GetUtcOffset(dateTime));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ public class Schedule
}

// Get the next execution date from the triggers.
return this.Triggers?
var next = this.Triggers?
.Select(t => t.GetNextExecution(after, timeZoneInfo))
.Where(d => d.HasValue)
.OrderBy(d => d)
.FirstOrDefault();
.Where(d => d.HasValue && d.Value.DateTime != DateTime.MinValue)
.OrderBy(d => d);
return next.Any() ? next.First() : null;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,12 @@ public WeeklyTrigger()
.Select(d => d.Value)
.Where(d => d > after.Value)
.OrderBy(d => d)
.Select(d => d.ToUniversalTime())
.ToList();

this.Logger?.Trace($"These are the potential matches: {string.Join(", ", potentialMatches)}");

return potentialMatches
.Select(d => d.ToUniversalTime())
.FirstOrDefault();
return potentialMatches.Any() ? (DateTimeOffset?)potentialMatches.First() : null;
}

/// <summary>
Expand Down

0 comments on commit 843fd90

Please sign in to comment.