Skip to content

Commit cc3a63a

Browse files
authored
Add Calendar.RecurrenceRule (#510)
This commit adds the new `Calendar.RecurrenceRule` struct that was introduced in SF-0009. It doesn't implement enumeration of recurrences, that will be done in a separate commit. Resolves rdar://120559017.
1 parent f6dddc1 commit cc3a63a

File tree

3 files changed

+517
-1
lines changed

3 files changed

+517
-1
lines changed

Sources/FoundationEssentials/Calendar/Calendar.swift

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ public struct Calendar : Hashable, Equatable, Sendable {
10591059
}
10601060

10611061
/// A hint to the search algorithm to control the method used for searching for dates.
1062-
public enum MatchingPolicy : Sendable {
1062+
public enum MatchingPolicy : Sendable, Equatable {
10631063
/// If there is no matching time before the end of the next instance of the next higher component to the highest specified component in the `DateComponents` argument, the algorithm will return the next existing time which exists.
10641064
///
10651065
/// For example, during a daylight saving transition there may be no 2:37am. The result would then be 3:00am, if that does exist.
@@ -1477,6 +1477,39 @@ package struct WeekendRange: Equatable, Hashable {
14771477
}
14781478
}
14791479

1480+
@available(FoundationPreview 0.4, *)
1481+
extension Calendar.MatchingPolicy: Codable {
1482+
public init(from decoder: Decoder) throws {
1483+
let container = try decoder.singleValueContainer()
1484+
switch try container.decode(Int.self) {
1485+
case 0:
1486+
self = .nextTime
1487+
case 1:
1488+
self = .nextTimePreservingSmallerComponents
1489+
case 2:
1490+
self = .previousTimePreservingSmallerComponents
1491+
case 3:
1492+
self = .strict
1493+
default:
1494+
throw DecodingError.dataCorrupted(.init(codingPath: decoder.codingPath, debugDescription: "Unknown MatchingPolicy"))
1495+
}
1496+
}
1497+
1498+
public func encode(to encoder: Encoder) throws {
1499+
var container = encoder.singleValueContainer()
1500+
switch self {
1501+
case .nextTime:
1502+
try container.encode(0)
1503+
case .nextTimePreservingSmallerComponents:
1504+
try container.encode(1)
1505+
case .previousTimePreservingSmallerComponents:
1506+
try container.encode(2)
1507+
case .strict:
1508+
try container.encode(3)
1509+
}
1510+
}
1511+
}
1512+
14801513
// MARK: - Bridging
14811514
#if FOUNDATION_FRAMEWORK
14821515
@available(macOS 10.10, iOS 8.0, watchOS 2.0, tvOS 9.0, *)
@@ -1518,4 +1551,5 @@ extension NSCalendar : _HasCustomAnyHashableRepresentation {
15181551
return AnyHashable(self as Calendar)
15191552
}
15201553
}
1554+
15211555
#endif // FOUNDATION_FRAMEWORK

0 commit comments

Comments
 (0)