宽窄优行-由【嘉易行】项目成品而来
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//
//  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
 
#if os(Linux)
import Glibc
#else
import Darwin
#endif
 
// MARK: - Style
 
public extension RelativeFormatter {
 
    enum PluralForm: String {
        case zero, one, two, few, many, other
    }
 
    /// Style for formatter
    struct Style {
 
        /// Flavours supported by the style, specified in order.
        /// The first available flavour for specified locale is used.
        /// If no flavour is available `.long` is used instead (this flavour
        /// MUST be part of every lang structure).
        public var flavours: [Flavour]
 
        /// Gradation specify how the unit are evaluated in order to get the
        /// best one to represent a given amount of time interval.
        /// By default `convenient()` is used.
        public var gradation: Gradation = .convenient()
 
        /// Allowed time units the style can use. Some styles may not include
        /// some time units (ie. `.quarter`) because they are not useful for
        /// a given representation.
        /// If not specified all the following units are set:
        /// `.now, .minute, .hour, .day, .week, .month, .year`
        public var allowedUnits: [Unit]?
 
        /// Create a new style.
        ///
        /// - Parameters:
        ///   - flavours: flavours of the style.
        ///   - gradation: gradation rules.
        ///   - units: allowed units.
        public init(flavours: [Flavour], gradation: Gradation, allowedUnits units: [Unit]? = nil) {
            self.flavours = flavours
            self.gradation = gradation
            allowedUnits = (units ?? [.now, .minute, .hour, .day, .week, .month, .year])
        }
    }
 
    /// Return the default style for relative formatter.
    ///
    /// - Returns: style instance.
    static func defaultStyle() -> Style {
        return Style(flavours: [.longConvenient, .long], gradation: .convenient())
    }
 
    /// Return the time-only style for relative formatter.
    ///
    /// - Returns: style instance.
    static func timeStyle() -> Style {
        return Style(flavours: [.longTime], gradation: .convenient())
    }
    /// Return the twitter style for relative formatter.
    ///
    /// - Returns: style instance.
    static func twitterStyle() -> Style {
        return Style(flavours: [.tiny, .shortTime, .narrow, .shortTime], gradation: .twitter())
    }
 
}
 
// MARK: - Flavour
 
public extension RelativeFormatter {
 
    /// Supported flavours
    enum Flavour: String {
        case long                 = "long"
        case longTime             = "long_time"
        case longConvenient         = "long_convenient"
        case short                 = "short"
        case shortTime             = "short_time"
        case shortConvenient     = "short_convenient"
        case narrow             = "narrow"
        case tiny                 = "tiny"
        case quantify             = "quantify"
    }
 
}
 
// MARK: - Gradation
 
public extension RelativeFormatter {
 
    /// Gradation is used to define a set of rules used to get the best
    /// representation of a given elapsed time interval (ie. the best
    /// representation for 300 seconds is in minutes, 5 minutes specifically).
    /// Rules are executed in order by the parser and the best one (< elapsed interval)
    /// is returned to be used by the formatter.
    struct Gradation {
 
        /// A single Gradation rule specification
        // swiftlint:disable nesting
        public struct Rule {
 
            public enum ThresholdType {
                case value(_: Double?)
                case function(_: ((TimeInterval) -> (Double?)))
 
                func evaluateForTimeInterval(_ elapsed: TimeInterval) -> Double? {
                    switch self {
                    case .value(let value):         return value
                    case .function(let function):     return function(elapsed)
                    }
                }
 
            }
 
            public enum RoundingStrategy {
 
                case regularRound
                case ceiling
                case flooring
                case custom((Double) -> Double)
 
                func roundValue(_ value: Double) -> Double {
 
                    switch self {
                    case .regularRound:                 return round(value)
                    case .ceiling:                      return ceil(value)
                    case .flooring:                     return floor(value)
                    case .custom(let roundingFunction): return roundingFunction(value)
                    }
                }
            }
 
            /// The time unit to which the rule refers.
            /// It's used to evaluate the factor.
            public var unit: Unit
 
            /// Threhsold value of the unit. When a difference between two dates
            /// is less than the threshold the unit before this is the best
            /// candidate to represent the time interval.
            public var threshold: ThresholdType?
 
            /// Granuality threshold of the unit
            public var granularity: Double?
 
            /// The rounding strategy that should be used prior to generating the relative time
            public var roundingStrategy: RoundingStrategy
 
            /// Relation with a previous threshold
            public var thresholdPrevious: [Unit: Double]?
 
            /// You can specify a custom formatter for a rule which return the
            /// string representation of a data with your own pattern.
            // swiftlint:disable nesting
            public typealias CustomFormatter = ((DateRepresentable) -> (String))
            public var customFormatter: CustomFormatter?
 
            /// Create a new rule.
            ///
            /// - Parameters:
            ///   - unit: target time unit.
            ///   - threshold: threshold value.
            ///   - granularity: granularity value.
            ///   - prev: relation with a previous rule in gradation lsit.
            ///   - formatter: custom formatter.
            public init(_ unit: Unit,
                        threshold: ThresholdType?,
                        granularity: Double? = nil,
                        roundingStrategy: RoundingStrategy = .regularRound,
                        prev: [Unit: Double]? = nil,
                        formatter: CustomFormatter? = nil ) {
                self.unit = unit
                self.threshold = threshold
                self.granularity = granularity
                self.roundingStrategy = roundingStrategy
                self.thresholdPrevious = prev
                self.customFormatter = formatter
            }
 
        }
 
        /// Gradation rules
        var rules: [Rule]
 
        /// Number of gradation rules
        var count: Int { return rules.count }
 
        /// Subscript by unit.
        /// Return the first rule for given unit.
        ///
        /// - Parameter unit: unit to get.
        public subscript(_ unit: Unit) -> Rule? {
            return rules.first(where: { $0.unit == unit })
        }
 
        /// Subscript by index.
        /// Return the rule at given index, `nil` if index is invalid.
        ///
        /// - Parameter index: index
        public subscript(_ index: Int) -> Rule? {
            guard index < rules.count, index >= 0 else { return nil }
            return rules[index]
        }
 
        /// Create a new gradition with a given set of ordered rules.
        ///
        /// - Parameter rules: ordered rules.
        public init(_ rules: [Rule]) {
            self.rules = rules
        }
 
        /// Create a new gradation by removing the units from receiver which are not part of the given array.
        ///
        /// - Parameter units: units to keep.
        /// - Returns: a new filtered `Gradation` instance.
        public func filtered(byUnits units: [Unit]) -> Gradation {
            return Gradation(rules.filter { units.contains($0.unit) })
        }
 
        /// Canonical gradation rules
        public static func canonical() -> Gradation {
            return Gradation([
                Rule(.now, threshold: .value(0)),
                Rule(.second, threshold: .value(0.5)),
                Rule(.minute, threshold: .value(59.5)),
                Rule(.hour, threshold: .value(59.5 * 60.0)),
                Rule(.day, threshold: .value(23.5 * 60 * 60)),
                Rule(.week, threshold: .value(6.5 * Unit.day.factor)),
                Rule(.month, threshold: .value(3.5 * 7 * Unit.day.factor)),
                Rule(.year, threshold: .value(1.5 * Unit.month.factor))
                ])
        }
 
        /// Convenient gradation rules
        public static func convenient() -> Gradation {
            let list = Gradation([
                Rule(.now, threshold: .value(0)),
                Rule(.second, threshold: .value(1), prev: [.now: 1]),
                Rule(.minute, threshold: .value(45)),
                Rule(.minute, threshold: .value(2.5 * 60), granularity: 5),
                Rule(.halfHour, threshold: .value(22.5 * 60), granularity: 5),
                Rule(.hour, threshold: .value(42.5 * 60), prev: [.minute: 52.5 * 60]),
                Rule(.day, threshold: .value((20.5 / 24) * Unit.day.factor)),
                Rule(.week, threshold: .value(5.5 * Unit.day.factor)),
                Rule(.month, threshold: .value(3.5 * 7 * Unit.day.factor)),
                Rule(.year, threshold: .value(10.5 * Unit.month.factor))
                ])
            return list
        }
 
        /// Twitter gradation rules
        public static func twitter() -> Gradation {
            return Gradation([
                Rule(.now, threshold: .value(0)),
                Rule(.second, threshold: .value(1), prev: [.now: 1]),
                Rule(.minute, threshold: .value(45)),
                Rule(.hour, threshold: .value(59.5 * 60.0)),
                Rule(.hour, threshold: .value((1.days.timeInterval - 0.5 * 1.hours.timeInterval))),
                Rule(.day, threshold: .value((20.5 / 24) * Unit.day.factor)),
                Rule(.other, threshold: .function({ now in
                    // Jan 1st of the next year.
                    let nextYear = (Date(timeIntervalSince1970: now) + 1.years).dateAtStartOf(.year)
                    return (nextYear.timeIntervalSince1970 - now)
                }), formatter: { date in // "Apr 11, 2017"
                    return date.toFormat("MMM dd, yyyy")
                })
            ])
        }
 
    }
 
}
 
// MARK: - Unit
 
public extension RelativeFormatter {
 
    /// Units for relative formatter
    enum Unit: String {
        case now         = "now"
        case second     = "second"
        case minute     = "minute"
        case hour         = "hour"
        case halfHour     = "half_hour"
        case day         = "day"
        case week         = "week"
        case month         = "month"
        case year         = "year"
        case quarter     = "quarter"
        case other         = ""
 
        /// Factor of conversion of the unit to seconds
        public var factor: Double {
            switch self {
            case .now, .second: return 1
            case .minute:         return 1.minutes.timeInterval
            case .hour:         return 1.hours.timeInterval
            case .halfHour:     return (1.hours.timeInterval * 0.5)
            case .day:             return 1.days.timeInterval
            case .week:         return 1.weeks.timeInterval
            case .month:         return 1.months.timeInterval
            case .year:         return 1.years.timeInterval
            case .quarter:         return (91.days.timeInterval + 6.hours.timeInterval)
            case .other:        return 0
            }
        }
 
    }
 
}
 
internal extension Double {
 
    /// Return -1 if number is negative, 1 if positive
    var sign: Int {
        return (self < 0 ? -1 : 1)
    }
 
}