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
This commit is contained in:
Adam Fowler
2020-06-27 13:16:52 -07:00
committed by GitHub
parent b9224e2fb6
commit 5130278087
3 changed files with 35 additions and 3 deletions
@@ -80,10 +80,15 @@ public struct RFC5322DateTimeCoding: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)
guard let date = Self.dateFormatter.date(from: dateString) else {
var string = try container.decode(String.self)
// RFC5322 dates sometimes have the alphabetic version of the timezone in brackets after the numeric version. The date formatter
// fails to parse this so we need to remove this before parsing.
if let bracket = string.firstIndex(of: "(") {
string = String(string[string.startIndex ..< bracket].trimmingCharacters(in: .whitespaces))
}
guard let date = Self.dateFormatter.date(from: string) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription:
"Expected date to be in RFC5322 date-time format with fractional seconds, but `\(dateString)` does not forfill format")
"Expected date to be in RFC5322 date-time format with fractional seconds, but `\(string)` does not forfill format")
}
self.wrappedValue = date
}
@@ -93,6 +93,32 @@ class DateWrapperTests: XCTestCase {
XCTAssertEqual(event?.date.description, "2012-04-05 21:47:37 +0000")
}
func testRFC5322DateTimeCodingWrapperWithExtraTimeZoneSuccess() {
struct TestEvent: Decodable {
@RFC5322DateTimeCoding
var date: Date
}
let json = #"{"date":"Fri, 26 Jun 2020 03:04:03 -0500 (CDT)"}"#
var event: TestEvent?
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
XCTAssertEqual(event?.date.description, "2020-06-26 08:04:03 +0000")
}
func testRFC5322DateTimeCodingWrapperWithAlphabeticTimeZoneSuccess() {
struct TestEvent: Decodable {
@RFC5322DateTimeCoding
var date: Date
}
let json = #"{"date":"Fri, 26 Jun 2020 03:04:03 CDT"}"#
var event: TestEvent?
XCTAssertNoThrow(event = try JSONDecoder().decode(TestEvent.self, from: json.data(using: .utf8)!))
XCTAssertEqual(event?.date.description, "2020-06-26 08:04:03 +0000")
}
func testRFC5322DateTimeCodingWrapperFailure() {
struct TestEvent: Decodable {
@RFC5322DateTimeCoding
+1
View File
@@ -337,6 +337,7 @@ AWS Lambda functions can be invoked directly from the AWS Lambda console UI, AWS
* [APIGateway Proxy](https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html)
* [S3 Events](https://docs.aws.amazon.com/lambda/latest/dg/with-s3.html)
* [SES Events](https://docs.aws.amazon.com/lambda/latest/dg/services-ses.html)
* [SNS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sns.html)
* [SQS Events](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html)
* [CloudWatch Events](https://docs.aws.amazon.com/lambda/latest/dg/services-cloudwatchevents.html)