diff --git a/Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift b/Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift similarity index 50% rename from Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift rename to Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift index 0a2121a..28cc7bb 100644 --- a/Sources/AmiiboService/Internal/Transcoders/ISOTimestampTranscoder.swift +++ b/Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift @@ -15,19 +15,22 @@ import Foundation import OpenAPIRuntime -/// A type that allows the decoding and encoding of ISO timestamp dates, defined by the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` custom date format. -struct ISOTimestampTranscoder { +/// A type that allows the decoding and encoding of ISO dates, supporting both the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` timestamp format and the `yyyy-MM-dd` date-only format. +struct ISODateTimeTranscoder { // MARK: Properties - /// A formatter to use to decode and encode ISO timestamps dates. - private let dateFormatter: DateFormatter = .isoTimestamp + /// A formatter to use to decode and encode ISO timestamp dates. + private let timestampFormatter: DateFormatter = .isoTimestamp + + /// A formatter to use to decode and encode ISO date-only strings. + private let dateFormatter: DateFormatter = .isoDate } - // MARK: - DateTranscoder +// MARK: - DateTranscoder -extension ISOTimestampTranscoder: DateTranscoder { +extension ISODateTimeTranscoder: DateTranscoder { // MARK: Functions @@ -35,22 +38,26 @@ extension ISOTimestampTranscoder: DateTranscoder { /// - Parameter date: A date to encode. /// - Returns: A string representation of the date in `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` format. func encode(_ date: Date) throws -> String { - dateFormatter.string(from: date) + timestampFormatter.string(from: date) } - /// Decodes an ISO timestamp string into a date. + /// Decodes an ISO date string into a date, trying the timestamp format first and falling back to the date-only format. /// - Parameter string: A string to decode. /// - Returns: A date parsed from the string. /// - Throws: A `DecodingError` if the string cannot be parsed into a valid date. func decode(_ string: String) throws -> Date { - guard let date = dateFormatter.date(from: string) else { - throw DecodingError.dataCorrupted(.init( - codingPath: [], - debugDescription: "Expected an ISO timestamp with format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSS', but found '\(string)' instead." - )) + if let date = timestampFormatter.date(from: string) { + return date } - return date + if let date = dateFormatter.date(from: string) { + return date + } + + throw DecodingError.dataCorrupted(.init( + codingPath: [], + debugDescription: "Expected an ISO date with format 'yyyy-MM-dd'T'HH:mm:ss.SSSSSS' or 'yyyy-MM-dd', but found '\(string)' instead." + )) } } diff --git a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift index 0ba560b..81933ff 100644 --- a/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift +++ b/Sources/AmiiboService/Public/Clients/AmiiboLiveClient.swift @@ -35,7 +35,7 @@ public struct AmiiboLiveClient: Sendable { self.client = .init( serverURL: serverURL, - configuration: .init(dateTranscoder: ISOTimestampTranscoder()), + configuration: .init(dateTranscoder: ISODateTimeTranscoder()), transport: transport ) }