一系列bug

This commit is contained in:
DDIsFriend
2023-09-12 18:58:43 +08:00
parent 66a3708532
commit 220155ce89
63 changed files with 13460 additions and 10213 deletions

View File

@@ -0,0 +1,207 @@
//
// DDDate.swift
// DDDateKit_Private
// Created by DDIsFriend on 2023/9/8.
import Foundation
public let DDDATE = DDDate.default
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:String,locale:String? = nil,timeZoneIdentifier:String? = nil) -> String {
return DDDATE.getDateFormatter(format: format,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: "HH:mm:ss"))"
}
if isYesterday() == true {
return "昨天\(dd_toString(format: "HH:mm:ss"))"
}
return dd_toString(format: "yyyy-MM-dd HH:mm:ss")
}
}
extension String {
///
/// - Parameters:
/// - dateString:
/// - format: ,
/// - Returns: true
public func dd_compare(dateString:String,format:String) -> ComparisonResult? {
let date = dd_toDate(format: self)
let compareDate = dd_toDate(format: self)
guard date != nil,compareDate != nil else {
return nil
}
return date!.compare(compareDate!)
}
/// date
/// - Parameters:
/// - format:
/// - locale:
/// - timeZoneIdentifier:
/// - Returns: date
public func dd_toDate(format:String,locale:String? = nil,timeZoneIdentifier:String? = nil) -> Date? {
return DDDATE.getDateFormatter(format: format,locale: locale,timeZoneIdentifier: timeZoneIdentifier).date(from: self)
}
///
/// - Parameter format:
/// - Returns:
public func dd_timeIntervalSince1970(format:String) -> TimeInterval? {
let date = dd_toDate(format: format)
guard date != nil else {
return nil
}
return date!.timeIntervalSince1970
}
///
/// - Parameter format:
/// - Returns:
public func dd_timeIntervalSssSince1970(format:String) -> TimeInterval? {
let timeInterval = dd_timeIntervalSince1970(format: format)
guard timeInterval != nil else {
return nil
}
return timeInterval! * 1000
}
///
/// - Parameter format:
/// - Returns:
public func isYesterday(format:String) -> Bool {
let date = dd_toDate(format: format)
guard date != nil else {
return false
}
return date!.isYesterday()
}
///
/// - Parameter format:
/// - Returns:
public func isToday(format:String) -> Bool {
let date = dd_toDate(format: format)
guard date != nil else {
return false
}
return date!.isToday()
}
///
/// - Parameter format:
/// - Returns:
public func isTomorrow(format:String) -> Bool {
let date = dd_toDate(format: format)
guard date != nil else {
return false
}
return date!.isTomorrow()
}
///
/// - Parameters:
/// - format:
/// - Returns:
public func dd_pushNotificationDateFormatString(format:String) -> String? {
let date = dd_toDate(format: format)
return date?.dd_pushNotificationDateFormatString()
}
}