Skip to content

fix Calendar.RecurrenceRule #1284

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Sources/FoundationEssentials/Calendar/Calendar_Recurrence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ extension Calendar {
/// value is used as a lower bound for ``nextBaseRecurrenceDate()``.
let rangeLowerBound: Date?

/// The start date's nanoseconds component
let startDateNanoseconds: TimeInterval

/// How many occurrences have been found so far
var resultsFound = 0

Expand Down Expand Up @@ -232,6 +235,8 @@ extension Calendar {
}
var componentsForEnumerating = recurrence.calendar._dateComponents(components, from: start)

startDateNanoseconds = start.timeIntervalSince1970.remainder(dividingBy: 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it work if we modify components above to include nanoseconds so the information is readily available in componentsForEnumerating, so that we don't have to do this manual adjustment here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@itingliu that was my first attempt, but it doesn't work.

in my understanding, the issue is that Calendar.DatesByRecurring's underlying baseRecurrence (Calendar.DatesByMatching) will always return only whole-second-rounded dates (though this isn't documented, afaict), so we'd need to adjust that type's behaviour instead (which i decided against bc i wasn't sure whether this would affect any other dependent code, and i figured instead going with the simplest possible fix would be the best approach)


let expansionChangesDay = dayOfYearAction == .expand || dayOfMonthAction == .expand || weekAction == .expand || weekdayAction == .expand
let expansionChangesMonth = dayOfYearAction == .expand || monthAction == .expand || weekAction == .expand

Expand Down Expand Up @@ -422,6 +427,13 @@ extension Calendar {
recurrence._limitTimeComponent(.second, dates: &dates, anchor: anchor)
}

if startDateNanoseconds > 0 {
// `_dates(startingAfter:)` above returns whole-second dates,
// so we need to restore the nanoseconds value present in the original start date.
for idx in dates.indices {
dates[idx] += startDateNanoseconds
}
}
dates = dates.filter { $0 >= self.start }

if let limit = recurrence.end.date {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -803,4 +803,16 @@ final class GregorianCalendarRecurrenceRuleTests: XCTestCase {
]
XCTAssertEqual(results, expectedResults)
}

func testDailyRecurrenceRuleWithNonzeroNanosecondComponent() {
let start = Date(timeIntervalSince1970: 1746627600.5) // 2025-05-07T07:20:00.500-07:00
let rule = Calendar.RecurrenceRule.daily(calendar: gregorian, end: .afterOccurrences(2))
let results = Array(rule.recurrences(of: start))

let expectedResults: [Date] = [
start,
Date(timeIntervalSince1970: 1746714000.5), // 2025-05-08T07:20:00.500-07:00
]
XCTAssertEqual(results, expectedResults)
}
}