Skip to content

Commit 5130278

Browse files
authored
SES time zone (#135)
motivation: Sometimes common header date comes with an alphabetical timezone in brackets after the numeric timezone changes: * Support common header date format with brackets * Update readme with SES Event link
1 parent b9224e2 commit 5130278

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

Sources/AWSLambdaEvents/Utils/DateWrappers.swift

+8-3
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,15 @@ public struct RFC5322DateTimeCoding: Decodable {
8080

8181
public init(from decoder: Decoder) throws {
8282
let container = try decoder.singleValueContainer()
83-
let dateString = try container.decode(String.self)
84-
guard let date = Self.dateFormatter.date(from: dateString) else {
83+
var string = try container.decode(String.self)
84+
// RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter
85+
// fails to parse this so we need to remove this before parsing.
86+
if let bracket = string.firstIndex(of: "(") {
87+
string = String(string[string.startIndex ..< bracket].trimmingCharacters(in: .whitespaces))
88+
}
89+
guard let date = Self.dateFormatter.date(from: string) else {
8590
throw DecodingError.dataCorruptedError(in: container, debugDescription:
86-
"Expected date to be in RFC5322 date-time format with fractional seconds, but `\(dateString)` does not forfill format")
91+
"Expected date to be in RFC5322 date-time format with fractional seconds, but `\(string)` does not forfill format")
8792
}
8893
self.wrappedValue = date
8994
}

Tests/AWSLambdaEventsTests/Utils/DateWrapperTests.swift

+26
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,32 @@ class DateWrapperTests: XCTestCase {
9393
XCTAssertEqual(event?.date.description, "2012-04-05 21:47:37 +0000")
9494
}
9595

96+
func testRFC5322DateTimeCodingWrapperWithExtraTimeZoneSuccess() {
97+
struct TestEvent: Decodable {
98+
@RFC5322DateTimeCoding
99+
var date: Date
100+
}
101+
102+
let json = #"{"date":"Fri, 26 Jun 2020 03:04:03 -0500 (CDT)"}"#
103+
var event: TestEvent?
104+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
105+
106+
XCTAssertEqual(event?.date.description, "2020-06-26 08:04:03 +0000")
107+
}
108+
109+
func testRFC5322DateTimeCodingWrapperWithAlphabeticTimeZoneSuccess() {
110+
struct TestEvent: Decodable {
111+
@RFC5322DateTimeCoding
112+
var date: Date
113+
}
114+
115+
let json = #"{"date":"Fri, 26 Jun 2020 03:04:03 CDT"}"#
116+
var event: TestEvent?
117+
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
118+
119+
XCTAssertEqual(event?.date.description, "2020-06-26 08:04:03 +0000")
120+
}
121+
96122
func testRFC5322DateTimeCodingWrapperFailure() {
97123
struct TestEvent: Decodable {
98124
@RFC5322DateTimeCoding

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS
337337

338338
* [APIGateway Proxy](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html)
339339
* [S3 Events](https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html)
340+
* [SES Events](https://docs.aws.amazon.com/lambda/latest/dg/services-ses.html)
340341
* [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
341342
* [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
342343
* [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html)

0 commit comments

Comments
 (0)