func formatMessage(_ messages: [EMMessage]) -> [LXChatMsgModel] {
var results = [LXChatMsgModel]()
for msg in messages {
let time = TimeInterval(msg.timestamp)
let diff = (lastMsgTimeTag - time) / 1000
if diff > 300 ||
diff < -300 {
let timeModel = LXChatMsgModel()
timeModel.timeStr = LXDateHelper.messageTimeToNowFormat(timestamp: Double(msg.timestamp))
timeModel.modelType = .time
results.append(timeModel)
self.lastMsgTimeTag = TimeInterval(msg.timestamp)
}
let model = LXChatMsgModel(message: msg)
results.append(model)
}
return results
}
class func messageTimeToNowFormat(timestamp: Double) -> String {
let currentTime = Date().timeIntervalSince1970
let timeSta:TimeInterval = TimeInterval(timestamp / 1000)
//时间差
let reduceTime : TimeInterval = currentTime - timeSta
var format: String = "HH:mm"
let hours = Int(reduceTime / 3600)
if hours < 48 {
let timeDate = Date(timeIntervalSince1970: timeSta)
if timeDate.isToday {
format = "HH:mm"
}else if timeDate.isYesterday {
format = "昨天 HH:mm"
} else {
format = "EEEE HH:mm"
}
} else {
let days = Int(reduceTime / 3600 / 24)
if days < 7 {
format = "EEEE HH:mm"
} else {
format = "yyyy年MM月dd日 HH:mm"
}
}
let date = Date(timeIntervalSince1970: timeSta)
let dfmatter = DateFormatter()
dfmatter.dateFormat = format
return dfmatter.string(from: date)
}
网友评论