宽窄优行-由【嘉易行】项目成品而来
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
//  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: - Weekday
 
/// This define the weekdays for some functions.
public enum WeekDay: Int {
    case sunday = 1, monday, tuesday, wednesday, thursday, friday, saturday
 
    /// Returns the name of the day given a specific locale.
    /// For example, for the `Friday` enum value, the en_AU locale would return "Friday" and fr_FR would return "samedi"
    ///
    /// - Parameter locale: locale of the output, omit to use the `defaultRegion`'s locale.
    /// - Returns: display name
    public func name(style: SymbolFormatStyle = .`default`, locale: LocaleConvertible = SwiftDate.defaultRegion.locale) -> String {
        let region = Region(calendar: SwiftDate.defaultRegion.calendar, zone: SwiftDate.defaultRegion.timeZone, locale: locale)
        let formatter = DateFormatter.sharedFormatter(forRegion: region, format: nil)
 
        let idx = (self.rawValue - 1)
        switch style {
        case .default:                return formatter.weekdaySymbols[idx]
        case .defaultStandalone:    return formatter.standaloneWeekdaySymbols[idx]
        case .short:                return formatter.shortWeekdaySymbols[idx]
        case .standaloneShort:        return formatter.shortStandaloneWeekdaySymbols[idx]
        case .veryShort:            return formatter.veryShortWeekdaySymbols[idx]
        case .standaloneVeryShort:    return formatter.veryShortStandaloneWeekdaySymbols[idx]
        }
    }
 
    /// Adds a number of days to the current weekday and returns the new weekday.
    ///
    /// - Parameter months: number of months to add
    /// - Returns: new month.
    public func add(days: Int) -> WeekDay {
        let normalized = days % 7
        return WeekDay(rawValue: ((self.rawValue + normalized + 7 - 1) % 7) + 1)!
    }
 
    /// Subtracts a number of days from the current weekday and returns the new weekday.
    ///
    /// - Parameter months: number of days to subtract. May be negative, in which case it will be added
    /// - Returns: new weekday.
    public func subtract(days: Int) -> WeekDay {
        return add(days: -(days % 7))
    }
 
}
 
// MARK: - Year
 
public struct Year: CustomStringConvertible, Equatable {
    let year: Int
 
    public var description: String {
        return "\(self.year)"
    }
 
    /// Constructs a `Year` from the passed value.
    ///
    /// - Parameter year: year value. Can be negative.
    public init(_ year: Int) {
        self.year = year
    }
 
    /// Returns whether this year is a leap year
    ///
    /// - Returns: A boolean indicating whether this year is a leap year
    public func isLeap() -> Bool {
        return ((year & 3) == 0) && ((year % 100) != 0 || (year % 400) == 0)
    }
 
    /// Returns the number of days in this year
    ///
    /// - Returns: The number of days in this year
    public func numberOfDays() -> Int {
        return self.isLeap() ? 366 : 365
    }
 
}
 
// MARK: - Month
 
/// Defines months in a year
public enum Month: Int, CustomStringConvertible, Equatable {
    case january = 0, february, march, april, may, june, july, august, september, october, november, december
 
    public var description: String {
        return self.name()
    }
 
    /// Returns the name of the month given a specific locale.
    /// For example, for the `January` enum value, the en_AU locale would return "January" and fr_FR would return "janvier"
    ///
    /// - Parameter locale: locale of the output, omit to use the `defaultRegion`'s locale.
    /// - Returns: display name
    public func name(style: SymbolFormatStyle = .`default`, locale: LocaleConvertible = SwiftDate.defaultRegion.locale) -> String {
        let region = Region(calendar: SwiftDate.defaultRegion.calendar, zone: SwiftDate.defaultRegion.timeZone, locale: locale)
        let formatter = DateFormatter.sharedFormatter(forRegion: region, format: nil)
        switch style {
        case .default:                return formatter.monthSymbols[self.rawValue]
        case .defaultStandalone:    return formatter.standaloneMonthSymbols[self.rawValue]
        case .short:                return formatter.shortMonthSymbols[self.rawValue]
        case .standaloneShort:        return formatter.shortStandaloneMonthSymbols[self.rawValue]
        case .veryShort:            return formatter.veryShortMonthSymbols[self.rawValue]
        case .standaloneVeryShort:    return formatter.veryShortStandaloneMonthSymbols[self.rawValue]
        }
    }
 
    /// Adds a number of months to the current month and returns the new month.
    ///
    /// - Parameter months: number of months to add
    /// - Returns: new month.
    public func add(months: Int) -> Month {
        let normalized = months % 12
        return Month(rawValue: (self.rawValue + normalized + 12) % 12)!
    }
 
    /// Subtracts a number of months from the current month and returns the new month.
    ///
    /// - Parameter months: number of months to subtract. May be negative, in which case it will be added
    /// - Returns: new month.
    public func subtract(months: Int) -> Month {
        return add(months: -(months % 12))
    }
 
    /// Returns the number of days in a this month for a given year
    ///
    /// - Parameter year: reference year.
    /// - Returns: The number of days in this month.
    public func numberOfDays(year: Int) -> Int {
        switch self {
        case .february:
            return Year(year).isLeap() ? 29 : 28
        case .april, .june, .september, .november:
            return 30
        default:
            return 31
        }
    }
 
}