@@ -30,32 +30,27 @@ public struct ISO8601Coding: Decodable, Sendable {
30
30
let container = try decoder. singleValueContainer ( )
31
31
let dateString = try container. decode ( String . self)
32
32
33
- struct InvalidDateError : Error { }
34
-
35
- do {
36
- self . wrappedValue = try Self . parseISO8601 ( dateString: dateString)
37
- } catch {
33
+ guard let date = Self . parseISO8601 ( dateString: dateString) else {
38
34
throw DecodingError . dataCorruptedError (
39
35
in: container,
40
36
debugDescription:
41
37
" Expected date to be in ISO8601 date format, but ` \( dateString) ` is not in the correct format "
42
38
)
43
39
}
40
+
41
+ self . wrappedValue = date
44
42
}
45
43
46
- private static func parseISO8601( dateString: String ) throws -> Date {
44
+ private static func parseISO8601( dateString: String ) -> Date ? {
45
+ #if canImport(FoundationEssentials)
47
46
if #available( macOS 12 . 0 , * ) {
48
- return try Date ( dateString, strategy: . iso8601)
47
+ return try ? Date ( dateString, strategy: . iso8601)
49
48
} else {
50
- #if !canImport(FoundationEssentials)
51
- guard let date = Self . dateFormatter. date ( from: dateString) else {
52
- throw InvalidDateError ( )
53
- }
54
- return date
55
- #endif
56
-
57
- fatalError ( " ISO8601Coding is not supported on this platform - this should never happen " )
49
+ return Self . dateFormatter. date ( from: dateString)
58
50
}
51
+ #else
52
+ return Self . dateFormatter. date ( from: dateString)
53
+ #endif
59
54
}
60
55
61
56
#if !canImport(FoundationEssentials)
@@ -81,35 +76,35 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
81
76
let container = try decoder. singleValueContainer ( )
82
77
let dateString = try container. decode ( String . self)
83
78
84
- struct InvalidDateError : Error { }
85
-
86
- do {
87
- self . wrappedValue = try Self . parseISO8601WithFractionalSeconds ( dateString: dateString)
88
- } catch {
79
+ guard let date = Self . parseISO8601WithFractionalSeconds ( dateString: dateString) else {
89
80
throw DecodingError . dataCorruptedError (
90
81
in: container,
91
82
debugDescription:
92
83
" Expected date to be in ISO8601 date format with fractional seconds, but ` \( dateString) ` is not in the correct format "
93
84
)
94
85
}
86
+
87
+ self . wrappedValue = date
95
88
}
96
89
97
- private static func parseISO8601WithFractionalSeconds( dateString: String ) throws -> Date {
90
+ private static func parseISO8601WithFractionalSeconds( dateString: String ) -> Date ? {
91
+ #if canImport(FoundationEssentials)
98
92
if #available( macOS 12 . 0 , * ) {
99
- return try Date ( dateString, strategy: Self . iso8601WithFractionalSeconds)
93
+ return try ? Date ( dateString, strategy: Self . iso8601WithFractionalSeconds)
100
94
} else {
101
- #if !canImport(FoundationEssentials)
102
- guard let date = Self . dateFormatter. date ( from: dateString) else {
103
- throw InvalidDateError ( )
104
- }
105
- return date
106
- #endif
107
-
108
- fatalError ( " ISO8601Coding is not supported on this platform - this should never happen " )
95
+ return Self . dateFormatter. date ( from: dateString)
109
96
}
97
+ #else
98
+ return Self . dateFormatter. date ( from: dateString)
99
+ #endif
110
100
}
111
101
112
- #if !canImport(FoundationEssentials)
102
+ #if canImport(FoundationEssentials)
103
+ @available ( macOS 12 . 0 , * )
104
+ private static var iso8601WithFractionalSeconds : Date . ISO8601FormatStyle {
105
+ Date . ISO8601FormatStyle ( includingFractionalSeconds: true )
106
+ }
107
+ #else
113
108
private static var dateFormatter : DateFormatter {
114
109
let formatter = DateFormatter ( )
115
110
formatter. locale = Locale ( identifier: " en_US_POSIX " )
@@ -118,11 +113,6 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
118
113
return formatter
119
114
}
120
115
#endif
121
-
122
- @available ( macOS 12 . 0 , * )
123
- private static var iso8601WithFractionalSeconds : Date . ISO8601FormatStyle {
124
- Date . ISO8601FormatStyle ( includingFractionalSeconds: true )
125
- }
126
116
}
127
117
128
118
@propertyWrapper
@@ -138,11 +128,15 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
138
128
let string = try container. decode ( String . self)
139
129
140
130
do {
131
+ #if canImport(FoundationEssentials)
141
132
if #available( macOS 12 . 0 , * ) {
142
133
self . wrappedValue = try Date ( string, strategy: Self . rfc5322DateParseStrategy)
143
134
} else {
144
135
self . wrappedValue = try Self . rfc5322DateParseStrategy. parse ( string)
145
136
}
137
+ #else
138
+ self . wrappedValue = try Self . rfc5322DateParseStrategy. parse ( string)
139
+ #endif
146
140
} catch {
147
141
throw DecodingError . dataCorruptedError (
148
142
in: container,
@@ -152,6 +146,7 @@ public struct RFC5322DateTimeCoding: Decodable, Sendable {
152
146
}
153
147
}
154
148
155
- private static let rfc5322DateParseStrategy = RFC5322DateParseStrategy ( calendar: Calendar ( identifier: . gregorian) )
156
-
149
+ private static var rfc5322DateParseStrategy : RFC5322DateParseStrategy {
150
+ RFC5322DateParseStrategy ( calendar: Calendar ( identifier: . gregorian) )
151
+ }
157
152
}
0 commit comments