From 047ccfa7548361584cae30d70669acbbed7ad0c3 Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Tue, 11 Feb 2025 16:35:56 +0300 Subject: [PATCH] fix: correct calculate next occurrence --- .../Common/Services/CronUpdateSkipService.cs | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/Endpoint/Common/Services/CronUpdateSkipService.cs b/Endpoint/Common/Services/CronUpdateSkipService.cs index 8ecfa23..fc766d1 100644 --- a/Endpoint/Common/Services/CronUpdateSkipService.cs +++ b/Endpoint/Common/Services/CronUpdateSkipService.cs @@ -45,32 +45,31 @@ public static class CronUpdateSkipService if (depth <= 0) return []; - currentDate ??= DateOnly.FromDateTime(DateTime.Now); - DateTimeOffset nextRunTime = currentDate.Value.ToDateTime(TimeOnly.FromDateTime(DateTime.Now)); - + DateTimeOffset nextRunTime = (currentDate?.ToDateTime(TimeOnly.MinValue) ?? DateTime.Now).ToUniversalTime(); List result = []; do { - var lastSkip = data.FilterDateEntry(nextRunTime.DateTime).LastOrDefault(); + var lastSkippedEntry = data.FilterDateEntry(nextRunTime.DateTime).LastOrDefault(); - if (lastSkip is { Start: not null, End: not null }) - nextRunTime = new DateTimeOffset(lastSkip.End.Value.AddDays(1), new TimeOnly(0, 0, 0), TimeSpan.Zero); - else if (lastSkip.Date.HasValue) - nextRunTime = new DateTimeOffset(lastSkip.Date.Value.AddDays(1), new TimeOnly(0, 0, 0), TimeSpan.Zero); + if (lastSkippedEntry is { Start: not null, End: not null }) + nextRunTime = lastSkippedEntry.End.Value.ToDateTime(TimeOnly.MinValue).AddDays(1); + else if (lastSkippedEntry.Date.HasValue) + nextRunTime = lastSkippedEntry.Date.Value.ToDateTime(TimeOnly.MinValue).AddDays(1); - var next = expression.GetNextOccurrence(nextRunTime.ToUniversalTime(), TimeZoneInfo.Local); + var nextOccurrence = expression.GetNextOccurrence(nextRunTime.AddMinutes(-1), TimeZoneInfo.Local); - if (!next.HasValue) + if (!nextOccurrence.HasValue) return result; - nextRunTime = next.Value; - - if (data.FilterDateEntry(nextRunTime.DateTime).Any()) + if (data.FilterDateEntry(nextOccurrence.Value.DateTime).Count != 0) + { + nextRunTime = nextOccurrence.Value.AddDays(1); continue; + } - result.Add(nextRunTime); - nextRunTime = nextRunTime.AddMinutes(1); + result.Add(nextOccurrence.Value.ToLocalTime()); + nextRunTime = nextOccurrence.Value.AddMinutes(1); } while (result.Count < depth);