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

49 lines
1.6 KiB
Swift
Raw Normal View History

2025-10-07 22:32:54 +00:00
// ===----------------------------------------------------------------------===
//
// This source file is part of the Amiibo Service open source project
//
2026-03-22 23:39:48 +00:00
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
2025-10-07 22:32:54 +00:00
// Licensed under Apache license v2.0
//
// See LICENSE for license information
2025-10-07 22:32:54 +00:00
// See CONTRIBUTORS for the list of Amiibo Service project authors
//
2025-10-07 22:32:54 +00:00
// SPDX-License-Identifier: Apache-2.0
//
// ===----------------------------------------------------------------------===
import Foundation
import OpenAPIRuntime
2025-09-09 17:30:19 +00:00
/// 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 {
// MARK: Properties
2025-09-09 17:30:19 +00:00
/// A formatter to use to decode and encode ISO timestamps dates.
private let dateFormatter: DateFormatter = .isoTimestamp
}
// MARK: - DateTranscoder
extension ISOTimestampTranscoder: DateTranscoder {
// MARK: Functions
2026-03-22 23:39:48 +00:00
/// Encodes a date into an ISO timestamp string.
/// - 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)
}
2026-03-22 23:39:48 +00:00
/// Decodes an ISO timestamp string into a date.
/// - Parameter string: A string to decode.
/// - Returns: A date parsed from the string, or the Unix epoch if the string cannot be parsed.
func decode(_ string: String) throws -> Date {
dateFormatter.date(from: string) ?? .init()
}
}