Vla*_*pko 7 date nsdateformatter swift
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))
Run Code Online (Sandbox Code Playgroud)
结果
"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
}
}
Run Code Online (Sandbox Code Playgroud)
用法:
let formatter = MicrosecondPrecisionDateFormatter()
let date = Date(timeIntervalSince1970: 1490891661.074981)
let formattedString = formatter.string(from: date) // 2017-03-30T16:34:21.074981Z
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1234 次 |
最近记录: |