Parse dates without fractional seconds

This commit is contained in:
Justin Mazzocchi 2021-05-08 14:41:55 -07:00
parent 7296de1dba
commit 8ceb02ba35
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C

View file

@ -11,7 +11,8 @@ public final class MastodonDecoder: JSONDecoder {
let container = try decoder.singleValueContainer() let container = try decoder.singleValueContainer()
let dateString = try container.decode(String.self) let dateString = try container.decode(String.self)
guard let date = Self.dateFormatter.date(from: dateString) else { guard let date = Self.dateFormatter.date(from: dateString)
?? Self.dateFormatterWithoutFractionalSeconds.date(from: dateString) else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to parse ISO8601 date") throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to parse ISO8601 date")
} }
@ -28,4 +29,12 @@ public extension MastodonDecoder {
return dateFormatter return dateFormatter
}() }()
static let dateFormatterWithoutFractionalSeconds: ISO8601DateFormatter = {
let dateFormatter = ISO8601DateFormatter()
dateFormatter.formatOptions = [.withInternetDateTime]
return dateFormatter
}()
} }