mirror of
https://github.com/metabolist/metatext
synced 2024-12-27 02:02:38 +01:00
Use system ISO8601DateFormatter
This commit is contained in:
parent
d11d644674
commit
dae380bf6d
@ -7,11 +7,12 @@ final class ContentDatabaseJSONEncoder: JSONEncoder {
|
||||
override init() {
|
||||
super.init()
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
|
||||
dateFormatter.dateFormat = Constants.dateFormat
|
||||
dateEncodingStrategy = .formatted(dateFormatter)
|
||||
keyEncodingStrategy = .convertToSnakeCase
|
||||
outputFormatting = .sortedKeys
|
||||
dateEncodingStrategy = .custom { date, encoder in
|
||||
var container = encoder.singleValueContainer()
|
||||
|
||||
try container.encode(MastodonDecoder.dateFormatter.string(from: date))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,26 @@ public final class MastodonDecoder: JSONDecoder {
|
||||
public override init() {
|
||||
super.init()
|
||||
|
||||
let dateFormatter = DateFormatter()
|
||||
|
||||
dateFormatter.dateFormat = Constants.dateFormat
|
||||
dateDecodingStrategy = .formatted(dateFormatter)
|
||||
keyDecodingStrategy = .convertFromSnakeCase
|
||||
dateDecodingStrategy = .custom { decoder in
|
||||
let container = try decoder.singleValueContainer()
|
||||
let dateString = try container.decode(String.self)
|
||||
|
||||
guard let date = Self.dateFormatter.date(from: dateString) else {
|
||||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to parse ISO8601 date")
|
||||
}
|
||||
|
||||
return date
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public extension MastodonDecoder {
|
||||
static let dateFormatter: ISO8601DateFormatter = {
|
||||
let dateFormatter = ISO8601DateFormatter()
|
||||
|
||||
dateFormatter.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
||||
|
||||
return dateFormatter
|
||||
}()
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
// Copyright © 2020 Metabolist. All rights reserved.
|
||||
|
||||
import Foundation
|
||||
|
||||
public enum Constants {
|
||||
public static let dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
|
||||
}
|
Loading…
Reference in New Issue
Block a user