简体   繁体   English

几乎是ISO8601格式

[英]Almost iSO8601 format

I need to parse the following date in a JSON payload and it's almost in ISO8601 format.我需要在 JSON 有效载荷中解析以下日期,它几乎是 ISO8601 格式。

"2020-06-05 14:52:54 UTC"

To conform to ISO8601 it needs to be altered slightly.为了符合 ISO8601,它需要稍作改动。

"2020-06-05T14:52:54Z"

It's super annoying because I now have to make a customer date decoding strategy.这非常烦人,因为我现在必须制定客户日期解码策略。

static func make() -> JSONDecoder {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
//        decoder.dateDecodingStrategy = .iso8601
    decoder.dateDecodingStrategy = .custom({ decoder in
        let container = try decoder.singleValueContainer()
        let dateStr = try container.decode(String.self)
        guard let date = formatter.date(from: dateStr) else {
            preconditionFailure("Unexpected date format.")
        }
        return date
    })
    return decoder
}

I don't have control of the data source.我无法控制数据源。 Is there anything I can do to avoid a custom decoding strategy in this case?在这种情况下,我能做些什么来避免自定义解码策略吗?

Since you seem to just want to use a DateFormatter to parse the date string, use the formatted strategy.由于您似乎只想使用DateFormatter来解析日期字符串,因此请使用formatted策略。

let formatter = DateFormatter()

formatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzzz" 
// or 
// formatter.dateFormat = "yyyy-MM-dd HH:mm:ss 'UTC'"
// formatter.timeZone = .init(identifier: "UTC")

formatter.locale = Locale(identifier: "en_US_POSIX")
decoder.dateDecodingStrategy = .formatted(formatter)

An alternative to Sweeper's answer is to write an extension of DateFormatter Sweeper 答案的替代方法是编写DateFormatter的扩展

extension DateFormatter {
    static let almostISO8601Formatter: DateFormatter = {
        let formatter = DateFormatter()
        formatter.calendar = Calendar(identifier: .iso8601)
        formatter.locale = Locale(identifier: "en_US_POSIX")
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
        return formatter
    }()
}

Then your make() function simply becomes然后你的make() function 就变成了

static func make() -> JSONDecoder {
    let decoder = JSONDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    decoder.dateDecodingStrategy = .formatted(.almostISO8601Formatter)
    return decoder
}

Consider also to put make() (with a more meaningful name) in an extension of JSONDecoder还考虑将make() (具有更有意义的名称)放在JSONDecoder的扩展中

You could map your input into iso8601, to use an existing decoder:您可以将map 输入到 iso8601,以使用现有的解码器:

func iso8601ify(_ str: String) -> String {
    str.split(separator: " ")
        .prefix(2)
        .joined(separator: "T")
        .appending("Z")
}

This example, there's no error handling, of course这个例子,当然没有错误处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM