@@ -5,7 +5,8 @@ import Foundation
55 time period. Cycles are defined by the first day of their effectivity period.
66 */
77
8- public struct Cycle : Codable , CustomStringConvertible , Sendable , Identifiable , Equatable , Hashable {
8+ public struct Cycle : Codable , LosslessStringConvertible , Sendable , Identifiable , Equatable , Hashable
9+ {
910 /// The earliest reference cycle (not necessarily the earliest cycle for
1011 /// which data is available, but the earliest representable date for a
1112 /// cycle).
@@ -98,9 +99,24 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
9899
99100 public var id : String { description }
100101
102+ /// Creates a cycle by parsing a `YYYY-MM-DD` string.
103+ ///
104+ /// - Parameter description: A string in `YYYY-MM-DD` format.
105+ public init ? ( _ description: String ) {
106+ let parts = description. split ( separator: " - " )
107+ guard parts. count == 3 ,
108+ let year = UInt ( parts [ 0 ] ) ,
109+ let month = UInt8 ( parts [ 1 ] ) ,
110+ let day = UInt8 ( parts [ 2 ] ) ,
111+ month >= 1 , month <= 12 ,
112+ day >= 1 , day <= 31
113+ else { return nil }
114+ self . init ( year: year, month: month, day: day)
115+ }
116+
101117 /**
102118 Generates a cycle from a month, day, and year. Does not validate the cycle.
103-
119+
104120 - Parameter year: The cycle year (2020 or later.
105121 - Parameter month: The cycle month (1–12).
106122 - Parameter day: The cycle day (1–31).
@@ -113,7 +129,7 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
113129
114130 /**
115131 Returns the cycle whose effectivity period includes the given date.
116-
132+
117133 - Parameter date: The date to use.
118134 - Returns: The cycle covering that date, or `nil` if the date is before the
119135 ``datum`` date.
@@ -147,7 +163,7 @@ public struct Cycle: Codable, CustomStringConvertible, Sendable, Identifiable, E
147163
148164 /**
149165 Returns whether a given date falls within this cycle.
150-
166+
151167 - Parameter date: A date to check.
152168 - Returns: Whether the date falls within the cycle's effectivity period.
153169 */
0 commit comments