avatar
童琦杰
Nov 17, 2019Technology

Swift - 时间类处理

DateTime.swift
swift
import Foundation

class DateTime {

    static var now: DateTime {
        return DateTime(Date())
    }

    static func parse(dateString: String, format: String, defaultValue: DateTime) -> DateTime {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        dateFormatter.locale = .current
        if let date = dateFormatter.date(from: dateString) {
            return DateTime(date)
        }
        return defaultValue;
    }

    private let _date: Date

    init() {
        let components = DateComponents(year: 1, month: 1, day: 1, hour: 0, minute: 0, second: 0)
        self._date = Calendar.current.date(from: components)!
    }

    init(_ date: Date) {
        self._date = date
    }

    init(_ year: Int, _ month: Int, _ day: Int) {
        let components = DateComponents(year: year, month: month, day: day)
        self._date = Calendar.current.date(from: components)!
    }

    var timestamp: Int {
        return Int(_date.timeIntervalSince1970)
    }

    var year: Int {
        if let y = Calendar.current.dateComponents([.year], from: _date).year {
            return y
        }
        return 1
    }

    var month: Int {
        if let y = Calendar.current.dateComponents([.month], from: _date).month {
            return y
        }
        return 1
    }

    var day: Int {
        if let y = Calendar.current.dateComponents([.day], from: _date).day {
            return y
        }
        return 1
    }

    var hour: Int {
        if let y = Calendar.current.dateComponents([.hour], from: _date).hour {
            return y
        }
        return 0
    }

    var minute: Int {
        if let y = Calendar.current.dateComponents([.minute], from: _date).minute {
            return y
        }
        return 0
    }

    var second: Int {
        if let y = Calendar.current.dateComponents([.second], from: _date).second {
            return y
        }
        return 0
    }

    /* Sunday = 0 */
    var dayOfWeek: Int {
        if let y = Calendar.current.dateComponents([.weekday], from: _date).weekday {
            return y - 1
        }
        return 0
    }

    var date: DateTime {
        return DateTime(year, month, day)
    }

    func toString() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return dateFormatter.string(from: self._date)
    }

    func toString(_ format: String) -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = format
        return dateFormatter.string(from: self._date)
    }

    func addYears(_ years: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .year, value: years, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    func addMonths(_ months: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .month, value: months, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    func addDays(_ days: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .day, value: days, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    func addHours(_ hours: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .hour, value: hours, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    func addMinutes(_ minutes: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .minute, value: minutes, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    func addSeconds(_ seconds: Int) -> DateTime {
        if let d = Calendar.current.date(byAdding: .second, value: seconds, to: _date) {
            return DateTime(d)
        }
        return DateTime()
    }

    static func <(first: DateTime, second: DateTime) -> Bool {
        return first._date.compare(second._date) == .orderedAscending
    }

    static func >(first: DateTime, second: DateTime) -> Bool {
        return first._date.compare(second._date) == .orderedDescending
    }

    static func ==(first: DateTime, second: DateTime) -> Bool {
        return first._date.compare(second._date) == .orderedSame
    }

    static func <=(first: DateTime, second: DateTime) -> Bool {
        let result = first._date.compare(second._date)
        return result == .orderedAscending || result == .orderedSame
    }

    static func >=(first: DateTime, second: DateTime) -> Bool {
        let result = first._date.compare(second._date)
        return result == .orderedDescending || result == .orderedSame
    }

    static func -(first: DateTime, second: DateTime) -> TimeSpan {
        return TimeSpan(Int((first._date.timeIntervalSinceReferenceDate - second._date.timeIntervalSinceReferenceDate) * 1000))
    }

    static func +(first: DateTime, second: TimeSpan) -> DateTime {
        return first.addSeconds(Int(second.totalSeconds))
    }
}
TimeSpan.swift
swift
import Foundation

class TimeSpan {

    let milliseconds: Int

    init(_ milliseconds: Int) {
        self.milliseconds = milliseconds
    }

    var days: Int {
        return Int((milliseconds / (24 * 60 * 60 * 1000)))
    }

    var hours: Int {
        return Int((milliseconds / (60 * 60 * 1000)) % 24)
    }

    var minutes: Int {
        return Int((milliseconds / (60 * 1000)) % 60)
    }

    var seconds: Int {
        return Int((milliseconds / 1000) % 60)
    }

    var totalDays: Double {
        return Double(milliseconds) / Double(24 * 60 * 60 * 1000)
    }

    var totalHours: Double {
        return Double(milliseconds) / (Double)(60 * 60 * 1000)
    }

    var totalMinutes: Double {
        return Double(milliseconds) / (Double)(60 * 1000)
    }

    var totalSeconds: Double {
        return Double(milliseconds) / (Double)(1000)
    }

    func toString() -> String {
        return "Days:\(days);Hours:\(hours);Minutes:\(minutes);Seconds:\(seconds)"
    }

    static func ==(first: TimeSpan, second: TimeSpan) -> Bool {
        return first.milliseconds == second.milliseconds
    }

    static func >(first: TimeSpan, second: TimeSpan) -> Bool {
        return first.milliseconds > second.milliseconds
    }

    static func >=(first: TimeSpan, second: TimeSpan) -> Bool {
        return first.milliseconds >= second.milliseconds
    }

    static func <(first: TimeSpan, second: TimeSpan) -> Bool {
        return first.milliseconds < second.milliseconds
    }

    static func <=(first: TimeSpan, second: TimeSpan) -> Bool {
        return first.milliseconds <= second.milliseconds
    }
}
© 2015-2022 tongqijie.com 版权所有沪ICP备17000682号