Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify date formatting #159

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 17 additions & 42 deletions Sources/AWSLambdaEvents/Utils/DateWrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

import struct Foundation.Date
import class Foundation.DateFormatter
import class Foundation.ISO8601DateFormatter
import struct Foundation.Locale
import struct Foundation.TimeZone

@propertyWrapper
public struct ISO8601Coding: Decodable {
Expand All @@ -28,28 +28,22 @@ public struct ISO8601Coding: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)
guard let date = Self.decodeDate(from: dateString) else {
guard let date = Self.dateFormatter.date(from: dateString) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription:
"Expected date to be in ISO8601 date format, but `\(dateString)` is not in the correct format")
}
self.wrappedValue = date
}

private static func decodeDate(from string: String) -> Date? {
#if os(Linux)
return Self.dateFormatter.date(from: string)
#elseif os(macOS)
if #available(macOS 10.12, *) {
return Self.dateFormatter.date(from: string)
} else {
// unlikely *debugging* use case of swift 5.2+ on older macOS
preconditionFailure("Unsporrted macOS version")
}
#endif
}
private static let dateFormatter: DateFormatter = Self.createDateFormatter()

@available(macOS 10.12, *)
private static let dateFormatter = ISO8601DateFormatter()
private static func createDateFormatter() -> DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return formatter
}
}

@propertyWrapper
Expand All @@ -63,39 +57,20 @@ public struct ISO8601WithFractionalSecondsCoding: Decodable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self)
guard let date = Self.decodeDate(from: dateString) else {
guard let date = Self.dateFormatter.date(from: dateString) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription:
"Expected date to be in ISO8601 date format with fractional seconds, but `\(dateString)` is not in the correct format")
}
self.wrappedValue = date
}

private static func decodeDate(from string: String) -> Date? {
#if os(Linux)
return Self.dateFormatter.date(from: string)
#elseif os(macOS)
if #available(macOS 10.13, *) {
return self.dateFormatter.date(from: string)
} else {
// unlikely *debugging* use case of swift 5.2+ on older macOS
preconditionFailure("Unsporrted macOS version")
}
#endif
}

@available(macOS 10.13, *)
private static let dateFormatter: ISO8601DateFormatter = Self.createDateFormatter()
private static let dateFormatter: DateFormatter = Self.createDateFormatter()

@available(macOS 10.13, *)
private static func createDateFormatter() -> ISO8601DateFormatter {
let formatter = ISO8601DateFormatter()
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withColonSeparatorInTimeZone,
.withFractionalSeconds,
]
private static func createDateFormatter() -> DateFormatter {
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
return formatter
}
}
Expand Down