gpt4 book ai didi

swift - 如何配置 DateFormatter 以捕获微秒

转载 作者:搜寻专家 更新时间:2023-10-30 21:53:49 26 4
gpt4 key购买 nike

iOS Date() 以至少微秒的精度返回日期。
我通过调用 Date().timeIntervalSince1970 检查了这条语句,结果是 1490891661.074981

然后我需要将日期转换为精度为微秒的字符串。
我按以下方式使用 DateFormatter:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSSSSZZZZZ"
print(formatter.string(from: date))

结果是
“2017-03-30T16:34:21.075000Z”

现在如果我们比较两个结果:
1490891661.074981"2017-03-30T16:34:21.075000Z"
我们可以注意到 DateFormatter 将日期四舍五入到毫秒精度,同时仍然为微秒显示零。

有谁知道如何配置 DateFormatter 以便我可以保持微秒并获得正确的结果:"2017-03-30T16:34:21.074981Z"

最佳答案

感谢@MartinR 解决了我的前半部分问题,并感谢@ForestKunecke 为我提供了解决后半部分问题的技巧。

在他们的帮助下,我创建了现成的解决方案,以微秒精度将日期从字符串转换成日期,反之亦然:

public final class MicrosecondPrecisionDateFormatter: DateFormatter {

private let microsecondsPrefix = "."

override public init() {
super.init()
locale = Locale(identifier: "en_US_POSIX")
timeZone = TimeZone(secondsFromGMT: 0)
}

required public init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

override public func string(from date: Date) -> String {
dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
let components = calendar.dateComponents(Set([Calendar.Component.nanosecond]), from: date)

let nanosecondsInMicrosecond = Double(1000)
let microseconds = lrint(Double(components.nanosecond!) / nanosecondsInMicrosecond)

// Subtract nanoseconds from date to ensure string(from: Date) doesn't attempt faulty rounding.
let updatedDate = calendar.date(byAdding: .nanosecond, value: -(components.nanosecond!), to: date)!
let dateTimeString = super.string(from: updatedDate)

let string = String(format: "%@.%06ldZ",
dateTimeString,
microseconds)

return string
}

override public func date(from string: String) -> Date? {
dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"

guard let microsecondsPrefixRange = string.range(of: microsecondsPrefix) else { return nil }
let microsecondsWithTimeZoneString = String(string.suffix(from: microsecondsPrefixRange.upperBound))

let nonDigitsCharacterSet = CharacterSet.decimalDigits.inverted
guard let timeZoneRangePrefixRange = microsecondsWithTimeZoneString.rangeOfCharacter(from: nonDigitsCharacterSet) else { return nil }

let microsecondsString = String(microsecondsWithTimeZoneString.prefix(upTo: timeZoneRangePrefixRange.lowerBound))
guard let microsecondsCount = Double(microsecondsString) else { return nil }

let dateStringExludingMicroseconds = string
.replacingOccurrences(of: microsecondsString, with: "")
.replacingOccurrences(of: microsecondsPrefix, with: "")

guard let date = super.date(from: dateStringExludingMicroseconds) else { return nil }
let microsecondsInSecond = Double(1000000)
let dateWithMicroseconds = date + microsecondsCount / microsecondsInSecond

return dateWithMicroseconds
}
}

用法:

let formatter = MicrosecondPrecisionDateFormatter()
let date = Date(timeIntervalSince1970: 1490891661.074981)
let formattedString = formatter.string(from: date) // 2017-03-30T16:34:21.074981Z

关于swift - 如何配置 DateFormatter 以捕获微秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43123944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com