宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-06 a1ae6802080a22e6e6ce6d0935e95facb1daca5c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//
//  SwiftDate
//  Parse, validate, manipulate, and display dates, time and timezones in Swift
//
//  Created by Daniele Margutti
//   - Web: https://www.danielemargutti.com
//   - Twitter: https://twitter.com/danielemargutti
//   - Mail: hello@danielemargutti.com
//
//  Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//
 
import Foundation
 
// MARK: Int Extension
 
/// This allows us to transform a literal number in a `DateComponents` and use it in math operations
/// For example `5.days` will create a new `DateComponents` where `.day = 5`.
 
public extension Int {
 
    /// Internal transformation function
    ///
    /// - parameter type: component to use
    ///
    /// - returns: return self value in form of `DateComponents` where given `Calendar.Component` has `self` as value
    internal func toDateComponents(type: Calendar.Component) -> DateComponents {
        var dateComponents = DateComponents()
        dateComponents.setValue(self, for: type)
        return dateComponents
    }
 
    /// Create a `DateComponents` with `self` value set as nanoseconds
    var nanoseconds: DateComponents {
        return toDateComponents(type: .nanosecond)
    }
 
    /// Create a `DateComponents` with `self` value set as seconds
    var seconds: DateComponents {
        return toDateComponents(type: .second)
    }
 
    /// Create a `DateComponents` with `self` value set as minutes
    var minutes: DateComponents {
        return toDateComponents(type: .minute)
    }
 
    /// Create a `DateComponents` with `self` value set as hours
    var hours: DateComponents {
        return toDateComponents(type: .hour)
    }
 
    /// Create a `DateComponents` with `self` value set as days
    var days: DateComponents {
        return toDateComponents(type: .day)
    }
 
    /// Create a `DateComponents` with `self` value set as weeks
    var weeks: DateComponents {
        return toDateComponents(type: .weekOfYear)
    }
 
    /// Create a `DateComponents` with `self` value set as months
    var months: DateComponents {
        return toDateComponents(type: .month)
    }
 
    /// Create a `DateComponents` with `self` value set as years
    var years: DateComponents {
        return toDateComponents(type: .year)
    }
 
    /// Create a `DateComponents` with `self` value set as quarters
    var quarters: DateComponents {
        return toDateComponents(type: .quarter)
    }
 
}