Skip to content

Commit

Permalink
simplify date formatting (#159)
Browse files Browse the repository at this point in the history
motivation: use consistent date formatting across platforms

changes: use manually configured DateFormatter instead of ISO8601DateFormatter
  • Loading branch information
tomerd authored Aug 14, 2020
1 parent f731664 commit 8986809
Showing 1 changed file with 17 additions and 42 deletions.
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

0 comments on commit 8986809

Please sign in to comment.