Visual_Novel_iOS/crush/Crush/Src/Utils/Extensions/DateExt.swift

174 lines
4.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// 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 {
/// <1s>=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)
}
}