Skip to content

Commit 2101c95

Browse files
authored
Allow for RFC5322 dates without a day (#12)
* Allow for RFC5322 dates without a day * added comment
1 parent 1e9645a commit 2101c95

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

Sources/AWSLambdaEvents/Utils/DateWrappers.swift

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,26 @@ public struct RFC5322DateTimeCoding: Decodable {
9191
if let bracket = string.firstIndex(of: "(") {
9292
string = String(string[string.startIndex ..< bracket].trimmingCharacters(in: .whitespaces))
9393
}
94-
guard let date = Self.dateFormatter.date(from: string) else {
95-
throw DecodingError.dataCorruptedError(in: container, debugDescription:
96-
"Expected date to be in RFC5322 date-time format with fractional seconds, but `\(string)` is not in the correct format")
94+
for formatter in Self.dateFormatters {
95+
if let date = formatter.date(from: string) {
96+
self.wrappedValue = date
97+
return
98+
}
9799
}
98-
self.wrappedValue = date
100+
throw DecodingError.dataCorruptedError(in: container, debugDescription:
101+
"Expected date to be in RFC5322 date-time format, but `\(string)` is not in the correct format")
99102
}
100103

101-
private static let dateFormatter: DateFormatter = Self.createDateFormatter()
102-
private static func createDateFormatter() -> DateFormatter {
103-
let formatter = DateFormatter()
104-
formatter.dateFormat = "EEE, d MMM yyy HH:mm:ss z"
105-
formatter.locale = Locale(identifier: "en_US_POSIX")
106-
return formatter
104+
private static let dateFormatters: [DateFormatter] = Self.createDateFormatters()
105+
private static func createDateFormatters() -> [DateFormatter] {
106+
// rfc5322 dates received in SES mails sometimes do not include the day, so need two dateformatters
107+
// one with a day and one without
108+
let formatterWithDay = DateFormatter()
109+
formatterWithDay.dateFormat = "EEE, d MMM yyy HH:mm:ss z"
110+
formatterWithDay.locale = Locale(identifier: "en_US_POSIX")
111+
let formatterWithoutDay = DateFormatter()
112+
formatterWithoutDay.dateFormat = "d MMM yyy HH:mm:ss z"
113+
formatterWithoutDay.locale = Locale(identifier: "en_US_POSIX")
114+
return [formatterWithDay, formatterWithoutDay]
107115
}
108116
}

Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,19 @@ class DateWrapperTests: XCTestCase {
119119
XCTAssertEqual(event?.date.description, "2020-06-26 08:04:03 +0000")
120120
}
121121

122+
func testRFC5322DateTimeCodingWithoutDayWrapperSuccess() {
123+
struct TestEvent: Decodable {
124+
@RFC5322DateTimeCoding
125+
var date: Date
126+
}
127+
128+
let json = #"{"date":"5 Apr 2012 23:47:37 +0200"}"#
129+
var event: TestEvent?
130+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
131+
132+
XCTAssertEqual(event?.date.description, "2012-04-05 21:47:37 +0000")
133+
}
134+
122135
func testRFC5322DateTimeCodingWrapperFailure() {
123136
struct TestEvent: Decodable {
124137
@RFC5322DateTimeCoding
@@ -133,7 +146,7 @@ class DateWrapperTests: XCTestCase {
133146
}
134147

135148
XCTAssertEqual(context.codingPath.map(\.stringValue), ["date"])
136-
XCTAssertEqual(context.debugDescription, "Expected date to be in RFC5322 date-time format with fractional seconds, but `\(date)` is not in the correct format")
149+
XCTAssertEqual(context.debugDescription, "Expected date to be in RFC5322 date-time format, but `\(date)` is not in the correct format")
137150
XCTAssertNil(context.underlyingError)
138151
}
139152
}

0 commit comments

Comments
 (0)