mirror of
https://github.com/metabolist/metatext.git
synced 2024-11-25 01:31:02 +00:00
Use system ISO8601DateFormatter
This commit is contained in:
parent
d11d644674
commit
dae380bf6d
3 changed files with 25 additions and 15 deletions
|
@ -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 a new issue