From 21782cde3a5d92b8ed0ff707d8144b9e6cea12a7 Mon Sep 17 00:00:00 2001 From: baegteun Date: Sun, 7 Dec 2025 00:30:02 +0900 Subject: [PATCH] Fix afterOccurrences to be calculated from startDate in RecurrenceRule --- .../Calendar/Calendar_Recurrence.swift | 5 +++ .../CalendarRecurrenceRuleTests.swift | 38 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift b/Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift index 95cf30182..988f4ae02 100644 --- a/Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift +++ b/Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift @@ -373,6 +373,11 @@ extension Calendar { // If a range has been specified, we should skip a few extra // occurrences until we reach the start date if let baseRecurrenceLowerBound, nextDate < baseRecurrenceLowerBound { + resultsFound += 1 + if let limit = recurrence.end.occurrences, resultsFound >= limit { + finished = true + return + } continue } anchor = nextDate diff --git a/Tests/FoundationInternationalizationTests/CalendarRecurrenceRuleTests.swift b/Tests/FoundationInternationalizationTests/CalendarRecurrenceRuleTests.swift index 4422e570b..14895fc07 100644 --- a/Tests/FoundationInternationalizationTests/CalendarRecurrenceRuleTests.swift +++ b/Tests/FoundationInternationalizationTests/CalendarRecurrenceRuleTests.swift @@ -134,4 +134,42 @@ private struct CalendarRecurrenceRuleTests { ] #expect(results == expectedResults) } + + @Test func afterOccurrenceMatchingDays() { + let recurrenceRule = Calendar.RecurrenceRule.init( + calendar: gregorian, + frequency: .daily, + end: .afterOccurrences(3) + ) + + let startDate = Date(timeIntervalSince1970: 1764576000.0) // 2025-12-01T00:00:00-0800 + + let rangeStartDate = Date(timeIntervalSince1970: 1765353600.0) // 2025-12-10T00:00:00-0800 + let rangeEndDate = Date(timeIntervalSince1970: 1766217600.0) // 2025-12-20T00:00:00-0800 + + let result = Array(recurrenceRule.recurrences(of: startDate, in: rangeStartDate..