214 lines
7.0 KiB
Swift
214 lines
7.0 KiB
Swift
//
|
||
// DDDate.swift
|
||
// DDDateKit_Private
|
||
// Created by DDIsFriend on 2023/9/8.
|
||
|
||
|
||
import Foundation
|
||
|
||
public let DDDATE = DDDate.default
|
||
|
||
public enum DateFormatEnum : String {
|
||
case yyyyMMddHHmmssDefault = "yyyy-MM-dd HH:mm:ss"
|
||
case yyyyMMddDefault = "yyyy-MM-dd"
|
||
case HHmmssDefault = "HH:mm:ss"
|
||
}
|
||
|
||
open class DDDate {
|
||
public static let `default` = DDDate()
|
||
// 设置一个全局使用的
|
||
public var globalLocale : String = "zh_CN"
|
||
public var globalTimeZoneIdentifier : String = "UTC+8"
|
||
|
||
func getLocale(locale:String?) -> String {
|
||
var _locale = globalLocale
|
||
if let locale {
|
||
_locale = locale
|
||
}
|
||
return _locale
|
||
}
|
||
|
||
func getTimeZoneIdentifier(timeZoneIdentifier:String?) -> String {
|
||
var _timeZoneIdentifier = globalTimeZoneIdentifier
|
||
if let timeZoneIdentifier {
|
||
_timeZoneIdentifier = timeZoneIdentifier
|
||
}
|
||
return _timeZoneIdentifier
|
||
}
|
||
|
||
func getDateFormatter(format:String,locale:String? = nil,timeZoneIdentifier:String? = nil) -> DateFormatter {
|
||
let dateFormatter = DateFormatter()
|
||
dateFormatter.dateFormat = format
|
||
dateFormatter.calendar = Calendar(identifier: .gregorian)
|
||
dateFormatter.locale = Locale(identifier: getLocale(locale: locale))
|
||
dateFormatter.timeZone = TimeZone.init(identifier: getTimeZoneIdentifier(timeZoneIdentifier: timeZoneIdentifier))
|
||
return dateFormatter
|
||
}
|
||
}
|
||
|
||
extension Date {
|
||
|
||
/// 返回一个毫秒的时间戳
|
||
/// - Returns: 时间戳
|
||
public func dd_timeintervalSssSince1970() -> TimeInterval {
|
||
let timeInterval = timeIntervalSince1970
|
||
return timeInterval * 1000
|
||
}
|
||
|
||
/// 将日期转成字符串
|
||
/// - Parameters:
|
||
/// - format: 指定字符串格式
|
||
/// - locale: 本地化
|
||
/// - timeZoneIdentifier: 时区
|
||
/// - Returns: 时间的字符串格式
|
||
public func dd_toString(format:DateFormatEnum,locale:String? = nil,timeZoneIdentifier:String? = nil) -> String {
|
||
return DDDATE.getDateFormatter(format: format.rawValue,locale: locale,timeZoneIdentifier: timeZoneIdentifier).string(from: self)
|
||
}
|
||
|
||
/// 判断date是不是昨天
|
||
/// - Returns: 是不是
|
||
public func isYesterday() -> Bool {
|
||
return Calendar.current.isDateInYesterday(self)
|
||
}
|
||
|
||
/// 判断date是不是今天
|
||
/// - Returns: 是不是
|
||
public func isToday() -> Bool {
|
||
return Calendar.current.isDateInToday(self)
|
||
}
|
||
|
||
/// 判断date是不是明天
|
||
/// - Returns: 是不是
|
||
public func isTomorrow() -> Bool {
|
||
return Calendar.current.isDateInTomorrow(self)
|
||
}
|
||
|
||
/// 指定一种固定模式,例如消息通知里时间的刚刚,今天,昨天以及指定日期模式
|
||
/// - Returns: 返回字符串格式,
|
||
public func dd_pushNotificationDateFormatString() -> String {
|
||
let currentDate = Date()
|
||
let duration = currentDate.timeIntervalSince1970 - timeIntervalSince1970
|
||
if duration < 0 {
|
||
return "时间穿越了"
|
||
}
|
||
|
||
if isToday() == true {
|
||
let min = Int(floor(duration / 60))
|
||
let hour = Int(floor(duration / 3600))
|
||
|
||
if duration < 60 {
|
||
return "刚刚"
|
||
}
|
||
|
||
// 当小时存在且是今天
|
||
if hour > 0 {
|
||
return "\(hour)小时前"
|
||
}
|
||
// 来到这里说明小时不存在
|
||
if min > 0 {
|
||
return "\(min)分钟前"
|
||
}
|
||
|
||
return "今天\(dd_toString(format: .HHmmssDefault))"
|
||
}
|
||
|
||
if isYesterday() == true {
|
||
return "昨天\(dd_toString(format: .HHmmssDefault))"
|
||
}
|
||
|
||
return dd_toString(format: .yyyyMMddHHmmssDefault)
|
||
}
|
||
}
|
||
|
||
extension String {
|
||
/// 当前时间的字符串和另一个字符串时间进行大小的比较
|
||
/// - Parameters:
|
||
/// - dateString: 比较的时间
|
||
/// - format: 比较时间的格式,时间比较需保持格式一致
|
||
/// - Returns: 大于返回true
|
||
public func dd_compare(dateString:String,format:DateFormatEnum) -> ComparisonResult? {
|
||
let date = dd_toDate(format: format)
|
||
let compareDate = dateString.dd_toDate(format: format)
|
||
guard date != nil,compareDate != nil else {
|
||
return nil
|
||
}
|
||
return date!.compare(compareDate!)
|
||
}
|
||
|
||
/// 将日期的字符串转成date
|
||
/// - Parameters:
|
||
/// - format: 当前字符串日期的格式
|
||
/// - locale: 本地化区域
|
||
/// - timeZoneIdentifier: 时区
|
||
/// - Returns: date
|
||
public func dd_toDate(format:DateFormatEnum,locale:String? = nil,timeZoneIdentifier:String? = nil) -> Date? {
|
||
return DDDATE.getDateFormatter(format: format.rawValue,locale: locale,timeZoneIdentifier: timeZoneIdentifier).date(from: self)
|
||
}
|
||
|
||
|
||
/// 将字符串转换成时间戳秒
|
||
/// - Parameter format: 字符串时间格式
|
||
/// - Returns: 时间戳
|
||
public func dd_timeIntervalSince1970(format:DateFormatEnum) -> TimeInterval? {
|
||
let date = dd_toDate(format: format)
|
||
guard date != nil else {
|
||
return nil
|
||
}
|
||
return date!.timeIntervalSince1970
|
||
}
|
||
|
||
/// 将字符串转换成时间戳毫秒
|
||
/// - Parameter format: 字符串时间格式
|
||
/// - Returns: 时间戳
|
||
public func dd_timeIntervalSssSince1970(format:DateFormatEnum) -> TimeInterval? {
|
||
let timeInterval = dd_timeIntervalSince1970(format: format)
|
||
guard timeInterval != nil else {
|
||
return nil
|
||
}
|
||
return timeInterval! * 1000
|
||
}
|
||
|
||
|
||
/// 当前时间是不是昨天
|
||
/// - Parameter format: 时间格式
|
||
/// - Returns: 是不是
|
||
public func isYesterday(format:DateFormatEnum) -> Bool {
|
||
let date = dd_toDate(format: format)
|
||
guard date != nil else {
|
||
return false
|
||
}
|
||
return date!.isYesterday()
|
||
}
|
||
|
||
/// 当前时间是不是今天
|
||
/// - Parameter format: 时间格式
|
||
/// - Returns: 是不是
|
||
public func isToday(format:DateFormatEnum) -> Bool {
|
||
let date = dd_toDate(format: format)
|
||
guard date != nil else {
|
||
return false
|
||
}
|
||
return date!.isToday()
|
||
}
|
||
|
||
/// 当前时间是不是明天
|
||
/// - Parameter format: 时间格式
|
||
/// - Returns: 是不是
|
||
public func isTomorrow(format:DateFormatEnum) -> Bool {
|
||
let date = dd_toDate(format: format)
|
||
guard date != nil else {
|
||
return false
|
||
}
|
||
return date!.isTomorrow()
|
||
}
|
||
|
||
/// 指定一种固定模式,例如消息通知里时间的刚刚,今天,昨天以及指定日期模式
|
||
/// - Parameters:
|
||
/// - format: 当前字符串时间的格式
|
||
/// - Returns: 字符串
|
||
public func dd_pushNotificationDateFormatString(format:DateFormatEnum) -> String? {
|
||
let date = dd_toDate(format: format)
|
||
return date?.dd_pushNotificationDateFormatString()
|
||
}
|
||
}
|