[英]Swift - Calculating elapsed time takes too long?
我的服务器调用为我提供了每个数据的日期时间的JSON数据,我想计算从现在到现在之间经过的时间并将其加载到我的数据结构中。 我正在做的方式现在需要很长时间,我应该使用不同的设计实践吗? 以下功能是我现在正在使用的功能
func dateDiff(_ dateStr:String) -> String {
var timeAgo = "10m"
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
formatter.timeZone = NSTimeZone(name: "AST") as! TimeZone
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
dateFormatter.timeZone = NSTimeZone(name: "AST") as! TimeZone
let now = formatter.string(from: Date())
if let date = formatter.date(from: dateStr){
if let nowDate = formatter.date(from: now){
let components = Calendar.current.dateComponents([.day,.hour,.minute,.second], from: date, to: nowDate)
let sec = components.second
let min = components.minute
let hours = components.hour
let days = components.day
if (sec! > 0){
if let secc = sec {
timeAgo = "\(secc)s"
}
}
if (min! > 0){
if let minn = min {
timeAgo = "\(minn)m"
} }
if(hours! > 0){
if let hourss = hours {
timeAgo = "\(hourss)h"
}
}
if(days! > 0){
if let dayss = days {
timeAgo = "\(dayss)d"
}
}
}
}
return timeAgo
}
每次调用dateDiff
创建然后使用DateFormatter对象都有些昂贵。 如果每个应用程序启动或JSON加载创建一个拥有dateDiff
的对象,那么优化的一件事就是使日期格式化程序成为类的成员,因此每次调用dateDiff
时都不会重新创建它。
换句话说,放:
var formatter : DateFormatter
var dateFormatter : DateFormatter
在类的顶部,然后在init
函数中初始化它。
你的代码开头太冗长了,你可以大大缩短它:
if let day = components.day, day > 0 {
timeAgo = "\(day)d"
} else if let hour = components.hour, hour > 0 {
timeAgo = "\(hour)h"
} else if let minute = components.minute, minute > 0 {
timeAgo = "\(minute)m"
} else if let second = components.second, second > 0 {
timeAgo = "\(second)s"
}
但更好的方法是使用自iOS 8和OS X 10.10以来可用的DateComponentsFormatter
:
let componentsFormatter = DateComponentsFormatter()
componentsFormatter.allowedUnits = [.second, .minute, .hour, .day]
componentsFormatter.maximumUnitCount = 1
componentsFormatter.unitsStyle = .abbreviated
let timeAgo = componentsFormatter.string(from: dateComponents)!
以下是它为您做的一些示例:
let dateComponents1 = DateComponents(calendar: .current, day: 1, hour: 4, minute: 6, second: 3)
let dateComponents2 = DateComponents(calendar: .current, day: 0, hour: 4, minute: 6, second: 3)
let dateComponents3 = DateComponents(calendar: .current, day: 0, hour: 0, minute: 6, second: 3)
let dateComponents4 = DateComponents(calendar: .current, day: 0, hour: 0, minute: 0, second: 3)
print(componentsFormatter.string(from: dateComponents1)!) // 1d
print(componentsFormatter.string(from: dateComponents2)!) // 4h
print(componentsFormatter.string(from: dateComponents3)!) // 6m
print(componentsFormatter.string(from: dateComponents4)!) // 3s
出于性能原因,您应该将日期格式化程序的实例化从方法中拉出来,因为这是众所周知的计算密集型。
我还建议使用DateComponentsFormatter
来简化已用时间的格式。
因此,定义两个格式化程序:
let dateFormatter: DateFormatter = {
let _formatter = DateFormatter()
_formatter.dateFormat = "yyyy-MM-dd' 'HH:mm:ss"
_formatter.locale = Locale(identifier: "en_US_POSIX")
_formatter.timeZone = TimeZone(abbreviation: "AST") // Curious; we usually use `TimeZone(secondsFromGMT: 0)` (i.e. GMT/UTC/Zulu)
return _formatter
}()
let componentsFormatter: DateComponentsFormatter = {
let _formatter = DateComponentsFormatter()
_formatter.maximumUnitCount = 1
_formatter.unitsStyle = .abbreviated
return _formatter
}()
然后你的功能大大简化了:
func dateDiff(_ string: String) -> String? {
guard let date = dateFormatter.date(from: string) else { return nil }
return componentsFormatter.string(from: date, to: Date())
}
另请注意:
TimeZone
,而不是通过NSTimeZone
进行NSTimeZone
; locale
为en_US_POSIX
,如果日期字符串的来源是Web服务或数据库,则应始终使用该locale
; Date()
; 唯一看起来可疑的事情是使用AST
作为时区。 通常日期字符串保存在GMT / UTC / Zulu中(例如,RFC 3339或ISO 8601)。 如果您可以控制,那可能是最佳做法,如果用户更改时区,可以避免出现问题;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.