Swift DateFormatter 可选毫秒数

Swift DateFormatter Optional Milliseconds

我有以下代码来解析 ISO8601 日期。

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"

问题是日期有时采用 2018-01-21T20:11:20.057Z 格式,有时采用 2018-01-21T20:11:20Z.

格式

所以基本上部分时间它有 .SSS 毫秒部分,而其他时间则没有。

如何设置日期格式化程序使该部分成为可选的?

编辑

我忘记提及我刚刚意识到的问题中的一些细节。所以我在 Swift 4 中使用了 JSON Codable 功能。所以如果它失败,它只会抛出一个错误。

所以我基本上有以下代码。

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .formatted(isoMilisecondDateFormatter())

return try decoder.decode([Person].self, from: _people)

_people 的示例 JSON 对象如下。

[
    {
        "name": "Bob",
        "born": "2018-01-21T20:11:20.057Z"
    },
    {
        "name": "Matt",
        "born": "2018-01-21T20:11:20Z"
    }
]

我正在使用的 API 非常不一致,所以我必须处理多种类型的数据。

两条建议:

  • 将字符串转换为包含毫秒的日期格式。如果 returns nil 将其转换为其他格式。

  • 使用正则表达式从字符串中去除毫秒数:

    var dateString = "2018-01-21T20:11:20.057Z"
    dateString = dateString.replacingOccurrences(of: "\.\d+", with: "", options: .regularExpression)
    // -> 2018-01-21T20:11:20Z
    

编辑:

要将它与 Codable 一起使用,您必须编写自定义初始化程序,指定 dateDecodingStrategy 不起作用

struct Foo: Decodable {
    let birthDate : Date
    let name : String

    private enum CodingKeys : String, CodingKey { case born, name }

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        var rawDate = try container.decode(String.self, forKey: .born)
        rawDate = rawDate.replacingOccurrences(of: "\.\d+", with: "", options: .regularExpression)
        birthDate = ISO8601DateFormatter().date(from: rawDate)!
        name = try container.decode(String.self, forKey: .name)
    }
}

let jsonString = """
[{"name": "Bob", "born": "2018-01-21T20:11:20.057Z"}, {"name": "Matt", "born": "2018-01-21T20:11:20Z"}]
"""

do {
    let data = Data(jsonString.utf8)
    let result = try JSONDecoder().decode([Foo].self, from: data)
    print(result)
} catch {
    print("error: ", error)
}

我创建了一个 DateFormatter 子类,它尝试使用小数秒进行解析,然后回退到第二个内部 DateFormatter,它不使用小数秒进行解析。

class OptionalFractionalSecondsDateFormatter: DateFormatter {

     // NOTE: iOS 11.3 added fractional second support to ISO8601DateFormatter, 
     // but it behaves the same as plain DateFormatter. It is either enabled
     // and required, or disabled and... anti-required
     // let formatter = ISO8601DateFormatter()
     // formatter.timeZone = TimeZone(secondsFromGMT: 0)
     // formatter.formatOptions = [.withInternetDateTime ] // .withFractionalSeconds

    static let withoutSeconds: DateFormatter = {
        let formatter = DateFormatter()
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.timeZone = TimeZone(identifier: "UTC")
        formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssXXX"
        return formatter
    }()

    func setup() {
        self.calendar = Calendar(identifier: .iso8601)
        self.locale = Locale(identifier: "en_US_POSIX")
        self.timeZone = TimeZone(identifier: "UTC")
        self.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSXXX" // handle up to 6 decimal places, although iOS currently only preserves 2 digits of precision
    }

    override init() {
        super.init()
        setup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setup()
    }

    override func date(from string: String) -> Date? {
        if let result = super.date(from: string) {
            return result
        }
        return OptionalFractionalSecondsDateFormatter.withoutSeconds.date(from: string)
    }
}

我保留了一份静态副本,因为它有点重。

extension DateFormatter {
    static let iso8601 = OptionalFractionalSecondsDateFormatter()
}

let str1 = "2018-05-10T21:41:30Z"
let str2 = "2018-05-10T21:41:30.54634Z"
let d1 = DateFormatter.iso8601.date(from: str1)
let d2 = DateFormatter.iso8601.date(from: str2)
DDLogInfo("d1 is \(String(describing: d1))")
DDLogInfo("d2 is \(String(describing: d2))")

显然,您可以自定义它以满足您自己的格式设置需要。特别是,您应该根据典型的日期格式构建两个解析器(无论您希望大部分是小数秒,还是大部分是整秒)