Files
amiibo-service/Sources/AmiiboService/Internal/Transcoders/ISODateTimeTranscoder.swift
T

59 lines
1.8 KiB
Swift
Raw Normal View History

2026-03-27 17:14:26 +00:00
// ===----------------------------------------------------------------------===
//
// This source file is part of the Amiibo Service open source project
//
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
// Licensed under Apache license v2.0
//
// See LICENSE for license information
// See CONTRIBUTORS for the list of Amiibo Service project authors
//
// SPDX-License-Identifier: Apache-2.0
//
// ===----------------------------------------------------------------------===
import Foundation
import OpenAPIRuntime
/// A transcoder for ISO dates in the `yyyy-MM-dd'T'HH:mm:ss.SSSSSS` timestamp and `yyyy-MM-dd` date-only formats.
2026-03-27 17:14:26 +00:00
struct ISODateTimeTranscoder {
// MARK: Properties
/// The ISO timestamp formatter.
2026-03-27 17:14:26 +00:00
private let timestampFormatter: DateFormatter = .isoTimestamp
/// The ISO date-only formatter.
2026-03-27 17:14:26 +00:00
private let dateFormatter: DateFormatter = .isoDate
}
// MARK: - DateTranscoder
extension ISODateTimeTranscoder: DateTranscoder {
// MARK: Functions
/// Encodes a date as an ISO timestamp string.
2026-03-27 17:14:26 +00:00
func encode(_ date: Date) throws -> String {
timestampFormatter.string(from: date)
}
/// Decodes an ISO date string, trying the timestamp format before the date-only one.
/// - Throws: A `DecodingError` if the string matches neither format.
2026-03-27 17:14:26 +00:00
func decode(_ string: String) throws -> Date {
if let date = timestampFormatter.date(from: string) {
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."
))
}
}