61 lines
1.8 KiB
Swift
61 lines
1.8 KiB
Swift
//
|
|
// DateParser.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 12/19/25.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
// MARK: - 내부: 다양한 포맷 파서를 시도
|
|
enum DateParser {
|
|
static func parse(_ text: String) -> Date? {
|
|
for parser in parsers {
|
|
if let d = parser(text) { return d }
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 시도 순서: ISO8601(소수초 포함) → ISO8601 → RFC3339 유사 → 공백 구분 기본
|
|
private static let parsers: [(String) -> Date?] = [
|
|
{ ISO8601.fractional.date(from: $0) },
|
|
{ ISO8601.basic.date(from: $0) },
|
|
{ DF.rfc3339.date(from: $0) },
|
|
{ DF.basic.date(from: $0) }
|
|
]
|
|
|
|
private enum ISO8601 {
|
|
static let fractional: ISO8601DateFormatter = {
|
|
let f = ISO8601DateFormatter()
|
|
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
|
|
f.timeZone = TimeZone(secondsFromGMT: 0)
|
|
return f
|
|
}()
|
|
|
|
static let basic: ISO8601DateFormatter = {
|
|
let f = ISO8601DateFormatter()
|
|
f.formatOptions = [.withInternetDateTime]
|
|
f.timeZone = TimeZone(secondsFromGMT: 0)
|
|
return f
|
|
}()
|
|
}
|
|
|
|
private enum DF {
|
|
static let rfc3339: DateFormatter = {
|
|
let f = DateFormatter()
|
|
f.locale = Locale(identifier: "en_US_POSIX")
|
|
f.timeZone = TimeZone(secondsFromGMT: 0)
|
|
f.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ"
|
|
return f
|
|
}()
|
|
|
|
static let basic: DateFormatter = {
|
|
let f = DateFormatter()
|
|
f.locale = Locale(identifier: "en_US_POSIX")
|
|
f.timeZone = TimeZone(secondsFromGMT: 0)
|
|
f.dateFormat = "yyyy-MM-dd HH:mm:ss"
|
|
return f
|
|
}()
|
|
}
|
|
}
|