174 lines
4.9 KiB
Swift
174 lines
4.9 KiB
Swift
//
|
||
// DateExt.swift
|
||
// Crush
|
||
//
|
||
// Created by Leon on 2025/7/15.
|
||
//
|
||
|
||
import Foundation
|
||
import SwiftDate
|
||
|
||
extension Date {
|
||
/// 获取当前 时间戳 - 默认毫秒
|
||
var timeStamp: Int {
|
||
let timeInterval: TimeInterval = timeIntervalSince1970
|
||
let millisecond = Int(round(timeInterval * 1000))
|
||
return millisecond
|
||
}
|
||
|
||
var timeStampInSeconds: Int {
|
||
let timeInterval: TimeInterval = timeIntervalSince1970
|
||
let timeStamp = Int(timeInterval)
|
||
return timeStamp
|
||
}
|
||
|
||
/// 获取当前 毫秒级 时间戳 - 13位
|
||
var milliStamp: Int {
|
||
let timeInterval: TimeInterval = timeIntervalSince1970
|
||
let millisecond = Int(round(timeInterval * 1000))
|
||
return millisecond
|
||
}
|
||
|
||
func toString(dateFormat: String = "yyyy-MM-dd HH:mm:ss") -> String {
|
||
let fm = DateFormatter()
|
||
fm.dateFormat = dateFormat
|
||
fm.locale = Locale(identifier: Languages.localRegionCode())
|
||
return fm.string(from: self)
|
||
}
|
||
|
||
static func dateFromMilliseconds(_ ms: Int64) -> Date {
|
||
return Date(timeIntervalSince1970: TimeInterval(ms) / 1000)
|
||
}
|
||
|
||
static func timerStyle(style: TimerStyle, millisecond: Int) -> String {
|
||
return Date.timerStyle(style: style, second: millisecond / 1000)
|
||
}
|
||
|
||
/// second: 秒
|
||
static func timerStyle(style: TimerStyle, second: Int) -> String {
|
||
if second == 0 {
|
||
return ""
|
||
}
|
||
let interval = TimeInterval(second)
|
||
let date = Date(timeIntervalSince1970: interval)
|
||
switch style {
|
||
case .WDMYHMS:
|
||
return date.toString(dateFormat: "E, d MMM yyyy HH:mm:ss")
|
||
case .DMYHMS:
|
||
return date.toString(dateFormat: "d MMM yyyy HH:mm:ss")
|
||
case .DMYHM:
|
||
return date.toString(dateFormat: "d MMM yyyy HH:mm")
|
||
case .DMHM:
|
||
return date.toString(dateFormat: "d MMM HH:mm")
|
||
case .DMY:
|
||
return date.toString(dateFormat: "d MMM yyyy")
|
||
case .DM:
|
||
return date.toString(dateFormat: "d MMM")
|
||
case .MY:
|
||
return date.toString(dateFormat: "MMM yyyy")
|
||
case .HM:
|
||
return date.toString(dateFormat: "HH:mm")
|
||
case .IMLIST:
|
||
return Date.timerStyleForIM(style: .IMLIST, second: second)
|
||
case .IMCHAT:
|
||
return Date.timerStyleForIM(style: .IMCHAT, second: second)
|
||
case .EVENTEND:
|
||
return date.toString(dateFormat: "d MMM HH:mm")
|
||
default:
|
||
return "-"
|
||
}
|
||
}
|
||
|
||
/// IM类型的时间格式 xx(分钟\小时\天\周\月\年)前
|
||
/// - Parameter timeStamp: 时间戳 13位
|
||
/// - Returns: String
|
||
private static func timerStyleForIM(style: TimerStyle, second: Int) -> String {
|
||
let interval = TimeInterval(second)
|
||
let last = Date(timeIntervalSince1970: interval)
|
||
// 今天
|
||
if last.isToday {
|
||
return last.toString(dateFormat: "HH:mm")
|
||
}
|
||
|
||
switch style {
|
||
case .IMLIST:
|
||
// 昨天
|
||
if last.isYesterday {
|
||
return "Yesterday"
|
||
}
|
||
// 本年不带年份
|
||
if last.compare(.isThisYear) {
|
||
return last.toString(dateFormat: "d MMM")
|
||
}
|
||
return last.toString(dateFormat: "d MMM yyyy")
|
||
|
||
case .IMCHAT:
|
||
// 昨天
|
||
if last.isYesterday {
|
||
return "Yesterday" + " " + last.toString(dateFormat: "HH:mm")
|
||
}
|
||
// 本年不带年份
|
||
if last.compare(.isThisYear) {
|
||
return last.toString(dateFormat: "d MMM HH:mm")
|
||
}
|
||
return last.toString(dateFormat: "d MMM yyyy HH:mm")
|
||
|
||
default:
|
||
return ""
|
||
}
|
||
}
|
||
}
|
||
|
||
enum TimerStyle {
|
||
case WDMYHMS
|
||
case DMYHMS
|
||
case DMYHM
|
||
case DMHM
|
||
case DMY
|
||
case DM
|
||
case MY
|
||
case HM
|
||
case IMLIST
|
||
case IMCHAT
|
||
case PUBLISH
|
||
case COMMENT
|
||
case EVENTEND
|
||
}
|
||
|
||
|
||
extension TimeInterval {
|
||
/// 格式化录音时长:<1分钟显示s,>=1分钟显示 m:ss
|
||
var imAIaudioDurationString: String {
|
||
// let totalSeconds = Int(self)
|
||
// let minutes = totalSeconds / 60
|
||
// let seconds = totalSeconds % 60
|
||
//
|
||
// if minutes > 0 {
|
||
// // 显示分钟:秒(秒补零)
|
||
// return String(format: "%d:%02d''", minutes, seconds)
|
||
// } else {
|
||
// // 小于1分钟只显示秒
|
||
// return "\(seconds)''"
|
||
// }
|
||
|
||
if self < 1 {
|
||
return "1''"
|
||
}
|
||
|
||
let totalSeconds = Int(self.rounded())
|
||
|
||
if totalSeconds < 60 {
|
||
return "\(totalSeconds)''"
|
||
}
|
||
|
||
let minutes = totalSeconds / 60
|
||
let seconds = totalSeconds % 60
|
||
|
||
if seconds == 0 {
|
||
return "\(minutes)'"
|
||
}
|
||
|
||
return String(format: "%d'%02d''", minutes, seconds)
|
||
}
|
||
}
|