@@ -33,13 +33,7 @@ public struct ISO8601Coding: Decodable, Sendable {
33
33
struct InvalidDateError : Error { }
34
34
35
35
do {
36
- if #available( macOS 12 . 0 , * ) {
37
- self . wrappedValue = try Date ( dateString, strategy: . iso8601)
38
- } else if let date = Self . dateFormatter. date ( from: dateString) {
39
- self . wrappedValue = date
40
- } else {
41
- throw InvalidDateError ( )
42
- }
36
+ self . wrappedValue = try Self . parseISO8601 ( dateString: dateString)
43
37
} catch {
44
38
throw DecodingError . dataCorruptedError (
45
39
in: container,
@@ -49,13 +43,32 @@ public struct ISO8601Coding: Decodable, Sendable {
49
43
}
50
44
}
51
45
46
+
47
+
48
+ private static func parseISO8601( dateString: String ) throws -> Date {
49
+ if #available( macOS 12 . 0 , * ) {
50
+ return try Date ( dateString, strategy: . iso8601)
51
+ } else {
52
+ #if !canImport(FoundationEssentials)
53
+ guard let date = Self . dateFormatter. date ( from: dateString) else {
54
+ throw InvalidDateError ( )
55
+ }
56
+ return date
57
+ #endif
58
+
59
+ fatalError ( " ISO8601Coding is not supported on this platform - this should never happen " )
60
+ }
61
+ }
62
+
63
+ #if !canImport(FoundationEssentials)
52
64
private static var dateFormatter : DateFormatter {
53
65
let formatter = DateFormatter ( )
54
66
formatter. locale = Locale ( identifier: " en_US_POSIX " )
55
67
formatter. timeZone = TimeZone ( secondsFromGMT: 0 )
56
68
formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ssZZZZZ "
57
69
return formatter
58
70
}
71
+ #endif
59
72
}
60
73
61
74
@propertyWrapper
@@ -73,13 +86,7 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
73
86
struct InvalidDateError : Error { }
74
87
75
88
do {
76
- if #available( macOS 12 . 0 , * ) {
77
- self . wrappedValue = try Date ( dateString, strategy: Self . iso8601WithFractionalSeconds)
78
- } else if let date = Self . dateFormatter. date ( from: dateString) {
79
- self . wrappedValue = date
80
- } else {
81
- throw InvalidDateError ( )
82
- }
89
+ self . wrappedValue = try Self . parseISO8601WithFractionalSeconds ( dateString: dateString)
83
90
} catch {
84
91
throw DecodingError . dataCorruptedError (
85
92
in: container,
@@ -89,13 +96,30 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
89
96
}
90
97
}
91
98
99
+ private static func parseISO8601WithFractionalSeconds( dateString: String ) throws -> Date {
100
+ if #available( macOS 12 . 0 , * ) {
101
+ return try Date ( dateString, strategy: Self . iso8601WithFractionalSeconds)
102
+ } else {
103
+ #if !canImport(FoundationEssentials)
104
+ guard let date = Self . dateFormatter. date ( from: dateString) else {
105
+ throw InvalidDateError ( )
106
+ }
107
+ return date
108
+ #endif
109
+
110
+ fatalError ( " ISO8601Coding is not supported on this platform - this should never happen " )
111
+ }
112
+ }
113
+
114
+ #if !canImport(FoundationEssentials)
92
115
private static var dateFormatter : DateFormatter {
93
116
let formatter = DateFormatter ( )
94
117
formatter. locale = Locale ( identifier: " en_US_POSIX " )
95
118
formatter. timeZone = TimeZone ( secondsFromGMT: 0 )
96
119
formatter. dateFormat = " yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ "
97
120
return formatter
98
121
}
122
+ #endif
99
123
100
124
@available ( macOS 12 . 0 , * )
101
125
private static var iso8601WithFractionalSeconds : Date . ISO8601FormatStyle {
0 commit comments