fix: use native ISO 8601 date formatter (#30)

Fixes: #29
This commit is contained in:
Thomas 2024-02-19 02:30:31 +00:00 committed by GitHub
parent 337faf3820
commit 0b20f2501f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 2 additions and 46 deletions

View File

@ -41,7 +41,8 @@ public final class JellyfinClient {
configuration.delegate = self
configuration.sessionDelegate = sessionDelegate
let isoDateFormatter: DateFormatter = OpenISO8601DateFormatter()
let isoDateFormatter: DateFormatter = ISO8601DateFormatter()
isoDateFormatter.formatOptions = .withFractionalSeconds
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(isoDateFormatter)

View File

@ -1,45 +0,0 @@
//
// jellyfin-sdk-swift is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at https://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2023 Jellyfin & Jellyfin Contributors
//
import Foundation
// https://stackoverflow.com/a/50281094/976628
public class OpenISO8601DateFormatter: DateFormatter {
static let withoutSeconds: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
return formatter
}()
private func setup() {
calendar = Calendar(identifier: .iso8601)
locale = Locale(identifier: "en_US_POSIX")
timeZone = TimeZone(secondsFromGMT: 0)
dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
}
override init() {
super.init()
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
override public func date(from string: String) -> Date? {
if let result = super.date(from: string) {
return result
}
return OpenISO8601DateFormatter.withoutSeconds.date(from: string)
}
}