Files
scan-receipts/app/ios/ScanReceipts/Support/ISO8601.swift

26 lines
987 B
Swift

import Foundation
/// The backend serialises every timestamp with `Date.prototype.toISOString()`,
/// which always includes milliseconds (`...307Z`). `ISO8601DateFormatter`'s
/// default options reject that string, so every date in this app must go
/// through this helper rather than a bare `ISO8601DateFormatter()` using
/// the wrong one is a silent-failure trap, not a compile error.
enum ISO8601 {
private static let withFractional: ISO8601DateFormatter = {
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime, .withFractionalSeconds]
return f
}()
private static let withoutFractional: ISO8601DateFormatter = {
let f = ISO8601DateFormatter()
f.formatOptions = [.withInternetDateTime]
return f
}()
static func parse(_ string: String?) -> Date? {
guard let string else { return nil }
return withFractional.date(from: string) ?? withoutFractional.date(from: string)
}
}