Skip to content

Add .count and .date properties to Calendar.RecurrenceRule.End #888

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
Merged
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
4 changes: 3 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ list(APPEND _SwiftFoundation_versions
"0.1"
"0.2"
"0.3"
"0.4")
"0.4"
"6.0.2"
)

# Each availability name to define
list(APPEND _SwiftFoundation_availability_names
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let availabilityTags: [_Availability] = [
_Availability("FoundationPredicate", availability: .macOS14_0), // Predicate relies on pack parameter runtime support
_Availability("FoundationPredicateRegex", availability: .macOS15_0) // Predicate regexes rely on new stdlib APIs
]
let versionNumbers = ["0.1", "0.2", "0.3", "0.4"]
let versionNumbers = ["0.1", "0.2", "0.3", "0.4", "6.0.2"]

// Availability Macro Utilities

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ extension Calendar {

dates = dates.filter { $0 >= self.start }

if let limit = recurrence.end.until {
if let limit = recurrence.end.date {
let hadDates = !dates.isEmpty
dates = dates.filter { $0 <= limit }
if hadDates && dates.isEmpty {
Expand Down Expand Up @@ -363,15 +363,15 @@ extension Calendar {

mutating func next() -> Element? {
guard !finished else { return nil }
if let limit = recurrence.end.count, resultsFound >= limit {
if let limit = recurrence.end.occurrences, resultsFound >= limit {
finished = true
return nil
}

while !finished {
if let date = currentGroup.popLast() {
resultsFound += 1
if let limit = recurrence.end.until, date > limit {
if let limit = recurrence.end.date, date > limit {
finished = true
return nil
}
Expand Down
28 changes: 23 additions & 5 deletions Sources/FoundationEssentials/Calendar/RecurrenceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,23 @@ extension Calendar {
public static var never: Self {
.init(_guts: .never)
}

internal var until: Date? {

/// At most many times the event may occur
/// This value is set when the struct was initialized with `.afterOccurrences()`
@available(FoundationPreview 6.0.2, *)
public var occurrences: Int? {
switch _guts {
case let .afterDate(date): date
case let .afterOccurrences(count): count
default: nil
}
}
internal var count: Int? {

/// The latest date when the event may occur
/// This value is set when the struct was initialized with `.afterDate()`
@available(FoundationPreview 6.0.2, *)
public var date: Date? {
switch _guts {
case let .afterOccurrences(count): count
case let .afterDate(date): date
default: nil
}
}
Expand Down Expand Up @@ -440,3 +447,14 @@ extension Calendar.RecurrenceRule: Codable {
try container.encode(setPositions, forKey: .setPositions)
}
}

@available(FoundationPreview 6.0.2, *)
extension Calendar.RecurrenceRule.End: CustomStringConvertible {
public var description: String {
switch self._guts {
case .never: "Never"
case .afterDate(let date): "After \(date)"
case .afterOccurrences(let n): "After \(n) occurrence(s)"
}
}
}