-
Couldn't load subscription status.
- Fork 205
[Proposal] SF-NNNN Search for recurrence in partial ranges #1457
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
Merged
itingliu
merged 1 commit into
swiftlang:main
from
hristost:hs/recurrence-rule-partial-ranges
Sep 4, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| # Search for recurrence in partial ranges | ||
|
|
||
| * Proposal: SF-NNNN | ||
| * Author: Hristo Staykov <https://github.com/hristost> | ||
| * Implementation: [#1456](https://github.com/swiftlang/swift-foundation/pull/1456) | ||
| * Status: **Draft** | ||
|
|
||
| ## Revision history | ||
|
|
||
| * **v1** Initial version | ||
|
|
||
| ## Introduction | ||
|
|
||
| In [SF-0009](0009-calendar-recurrence-rule.md) we introduced `Calendar.RecurrenceRule`. With this API, we can find occurences of a recurring event in a given range: | ||
|
|
||
| ```swift | ||
| let birthday = Date(timeIntervalSince1970: 813283200.0) // 1995-10-10T00:00:00-0000 | ||
| let rangeStart = Date(timeIntervalSince1970: 946684800.0) // 2000-01-01T00:00:00-0000 | ||
| let rangeEnd = Date(timeIntervalSince1970: 1293840000.0) // 2011-01-01T00:00:00-0000 | ||
|
|
||
| let recurrence = Calendar.RecurrenceRule(calendar: .current, frequency: .yearly) | ||
| for date in recurrence.recurrences(of: birthday, in: rangeStart..<rangeEnd) { | ||
| // All occurrences of `birthday` between 2000 and 2010 | ||
| } | ||
| ``` | ||
|
|
||
| However, enumerating recurrences in a partial range is not supported: the user has to enumerate over a larger range, and discard results that are not necessary: | ||
|
|
||
| ```swift | ||
| for date in recurrence.recurrences(of: birthday) where date >= rangeStart { | ||
| // All occurrences of `birthday` after 2000 | ||
| } | ||
| ``` | ||
|
|
||
| or specify a range that stretches to `Date.distantPast` or `Date.distantFuture`: | ||
|
|
||
| ```swift | ||
| for date in recurrence.recurrences(of: birthday, in: rangeStart..<Date.distantFuture) { | ||
| // All occurrences of `birthday` after 2000 | ||
| } | ||
| ``` | ||
|
|
||
| This proposal adds a method similar to `recurrences` that allows specifying partial ranges. | ||
|
|
||
|
|
||
| ## Detailed design | ||
|
|
||
| ```swift | ||
| public extension Calendar.RecurrenceRule.End { | ||
| @available(FoundationPreview 6.3, *) | ||
| public func recurrences(of start: Date, | ||
| in range: PartialRangeThrough<Date> | ||
| ) -> some (Sequence<Date> & Sendable) | ||
| @available(FoundationPreview 6.3, *) | ||
| public func recurrences(of start: Date, | ||
| in range: PartialRangeTo<Date> | ||
| ) -> some (Sequence<Date> & Sendable) | ||
| @available(FoundationPreview 6.3, *) | ||
| public func recurrences(of start: Date, | ||
| in range: PartialRangeFrom<Date> | ||
| ) -> some (Sequence<Date> & Sendable) | ||
| @available(FoundationPreview 6.3, *) | ||
| public func recurrences(of start: Date, | ||
| in range: ClosedRange<Date> | ||
| ) -> some (Sequence<Date> & Sendable) | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| With this, the above example would simply become: | ||
|
|
||
| ```swift | ||
| for date in recurrence.recurrences(of: birthday, in: rangeStart...) { | ||
| // All occurrences of `birthday` after 2000 | ||
| } | ||
| ``` | ||
|
|
||
|
|
||
| ## Impact on existing code | ||
|
|
||
| None. | ||
|
|
||
| ## Alternatives considered | ||
|
|
||
| This API is a convenience over the workarounds presented in the introduction, but it | ||
| is also more performant since we don't calculate dates we don't need in the final range. | ||
|
|
||
| For cases where we're looking for recurrences up until a date, it might be tempting to set the `end` property of the recurrence rule to the end of the range: | ||
| ```swift | ||
| recurrence.end = .afterDate(rangeEnd) | ||
| ``` | ||
| That is not advised for the recurrence rule might already have an `end` property of `.afterOcurrences()`. Besides, the recurrence rule struct represents when the event occurs, and the range in which we search is does not change that. | ||
|
|
||
| We did consider instead only adding one method that accepts a `RangeExpression<Date>` argument. However, that means supporting any arbitrary ranges conforming to the protocol, in which cases we may not have lower and upper bounds that allow us to optimize search. | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it ready?