Skip to content

Commit ef50f61

Browse files
committed
fix platform conditionals
1 parent f3e7c1f commit ef50f61

File tree

1 file changed

+38
-14
lines changed

1 file changed

+38
-14
lines changed

Sources/AWSLambdaEvents/Utils/DateWrappers.swift

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,7 @@ public struct ISO8601Coding: Decodable, Sendable {
3333
struct InvalidDateError: Error {}
3434

3535
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)
4337
} catch {
4438
throw DecodingError.dataCorruptedError(
4539
in: container,
@@ -49,13 +43,32 @@ public struct ISO8601Coding: Decodable, Sendable {
4943
}
5044
}
5145

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)
5264
private static var dateFormatter: DateFormatter {
5365
let formatter = DateFormatter()
5466
formatter.locale = Locale(identifier: "en_US_POSIX")
5567
formatter.timeZone = TimeZone(secondsFromGMT: 0)
5668
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
5769
return formatter
5870
}
71+
#endif
5972
}
6073

6174
@propertyWrapper
@@ -73,13 +86,7 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
7386
struct InvalidDateError: Error {}
7487

7588
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)
8390
} catch {
8491
throw DecodingError.dataCorruptedError(
8592
in: container,
@@ -89,13 +96,30 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable, Sendable {
8996
}
9097
}
9198

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)
92115
private static var dateFormatter: DateFormatter {
93116
let formatter = DateFormatter()
94117
formatter.locale = Locale(identifier: "en_US_POSIX")
95118
formatter.timeZone = TimeZone(secondsFromGMT: 0)
96119
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
97120
return formatter
98121
}
122+
#endif
99123

100124
@available(macOS 12.0, *)
101125
private static var iso8601WithFractionalSeconds: Date.ISO8601FormatStyle {

0 commit comments

Comments
 (0)