Skip to content

fix recurrence nanosecond handling for nanosecond values > 0.5 #1320

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
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ extension Calendar {
}
var componentsForEnumerating = recurrence.calendar._dateComponents(components, from: start)

startDateNanoseconds = start.timeIntervalSince1970.remainder(dividingBy: 1)
startDateNanoseconds = start.timeIntervalSinceReferenceDate.truncatingRemainder(dividingBy: 1)

let expansionChangesDay = dayOfYearAction == .expand || dayOfMonthAction == .expand || weekAction == .expand || weekdayAction == .expand
let expansionChangesMonth = dayOfYearAction == .expand || monthAction == .expand || weekAction == .expand
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -805,14 +805,19 @@ final class GregorianCalendarRecurrenceRuleTests: XCTestCase {
}

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)
let referenceStart = Date(timeIntervalSinceReferenceDate: 1746627600) // 2025-05-07T07:20:00.000-07:00
let referenceEnd = Date(timeIntervalSinceReferenceDate: 1746714000) // 2025-05-08T07:20:00.000-07:00

for nsec in stride(from: 0, through: 1, by: 0.05) {
let start = referenceStart.addingTimeInterval(nsec)
let rule = Calendar.RecurrenceRule.daily(calendar: gregorian, end: .afterOccurrences(2))
let results = Array(rule.recurrences(of: start))

let expectedResults: [Date] = [
start,
referenceEnd.addingTimeInterval(nsec)
]
XCTAssertEqual(results, expectedResults, "Failed for nanoseconds \(nsec)")
}
}
}