Files
OrderScheduling/Pods/DDDateKit_Private/DDDateKit_Private/Classes/DDDate.swift
DDIsFriend 004df124ed update
2023-09-19 11:04:22 +08:00

214 lines
7.0 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.

//
// 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()
}
}