Parse dates without fractional seconds
This commit is contained in:
parent
7296de1dba
commit
8ceb02ba35
|
@ -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
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue