|
7 | 7 |
|
8 | 8 | import Foundation
|
9 | 9 |
|
10 |
| -public enum DateUnit { |
11 |
| - |
12 |
| - case days(_ value: Int) |
13 |
| - case weeks(_ value: Int) |
14 |
| - case months(_ value: Int) |
15 |
| - case years(_ value: Int) |
| 10 | +/// `DateUnit` for use in adding and subtracting units from `Temporal.Date`, `Temporal.DateTime`, and `Temporal.Time`. |
| 11 | +/// |
| 12 | +/// `DateUnit` uses `Calendar.Component` under the hood to ensure date oddities are accounted for. |
| 13 | +/// |
| 14 | +/// let tomorrow = Temporal.Date.now() + .days(1) |
| 15 | +/// let twoWeeksFromNow = Temporal.Date.now() + .weeks(2) |
| 16 | +/// let nextYear = Temporal.Date.now() + .years(1) |
| 17 | +/// |
| 18 | +/// let yesterday = Temporal.Date.now() - .days(1) |
| 19 | +/// let sixMonthsAgo = Temporal.Date.now() - .months(6) |
| 20 | +public struct DateUnit { |
| 21 | + let calendarComponent: Calendar.Component |
| 22 | + let value: Int |
16 | 23 |
|
| 24 | + /// One day. Equivalent to 1 x `Calendar.Component.day` |
17 | 25 | public static let oneDay: DateUnit = .days(1)
|
| 26 | + |
| 27 | + /// One week. Equivalent to 7 x `Calendar.Component.day` |
18 | 28 | public static let oneWeek: DateUnit = .weeks(1)
|
| 29 | + |
| 30 | + /// One month. Equivalent to 1 x `Calendar.Component.month` |
19 | 31 | public static let oneMonth: DateUnit = .months(1)
|
| 32 | + |
| 33 | + /// One year. Equivalent to 1 x `Calendar.Component.year` |
20 | 34 | public static let oneYear: DateUnit = .years(1)
|
21 | 35 |
|
22 |
| - public var calendarComponent: Calendar.Component { |
23 |
| - switch self { |
24 |
| - case .days, .weeks: |
25 |
| - return .day |
26 |
| - case .months: |
27 |
| - return .month |
28 |
| - case .years: |
29 |
| - return .year |
30 |
| - } |
| 36 | + /// DateUnit amount of days. |
| 37 | + /// One day is 1 x `Calendar.Component.day` |
| 38 | + /// |
| 39 | + /// let fiveDays = DateUnit.days(5) |
| 40 | + /// // or |
| 41 | + /// let fiveDays: DateUnit = .days(5) |
| 42 | + /// |
| 43 | + /// - Parameter value: Amount of days in this `DateUnit` |
| 44 | + /// - Returns: A `DateUnit` with the defined number of days. |
| 45 | + public static func days(_ value: Int) -> Self { |
| 46 | + .init(calendarComponent: .day, value: value) |
| 47 | + } |
| 48 | + |
| 49 | + /// DateUnit amount of weeks. |
| 50 | + /// One week is 7 x the `Calendar.Component.day` |
| 51 | + /// |
| 52 | + /// let twoWeeks = DateUnit.weeks(2) |
| 53 | + /// // or |
| 54 | + /// let twoWeeks: DateUnit = .weeks(2) |
| 55 | + /// |
| 56 | + /// - Parameter value: Amount of weeks in this `DateUnit` |
| 57 | + /// - Returns: A `DateUnit` with the defined number of weeks. |
| 58 | + public static func weeks(_ value: Int) -> Self { |
| 59 | + .init(calendarComponent: .day, value: value * 7) |
31 | 60 | }
|
32 | 61 |
|
33 |
| - public var value: Int { |
34 |
| - switch self { |
35 |
| - case .days(let value), |
36 |
| - .months(let value), |
37 |
| - .years(let value): |
38 |
| - return value |
39 |
| - case .weeks(let value): |
40 |
| - return value * 7 |
41 |
| - } |
| 62 | + /// DateUnit amount of months. |
| 63 | + /// One month is 1 x `Calendar.Component.month` |
| 64 | + /// |
| 65 | + /// let sixMonths = DateUnit.months(6) |
| 66 | + /// // or |
| 67 | + /// let sixMonths: DateUnit = .months(6) |
| 68 | + /// |
| 69 | + /// - Parameter value: Amount of months in this `DateUnit` |
| 70 | + /// - Returns: A `DateUnit` with the defined number of months. |
| 71 | + public static func months(_ value: Int) -> Self { |
| 72 | + .init(calendarComponent: .month, value: value) |
42 | 73 | }
|
43 | 74 |
|
| 75 | + /// DateUnit amount of years. |
| 76 | + /// One year is 1 x `Calendar.Component.year` |
| 77 | + /// |
| 78 | + /// let oneYear = DateUnit.years(1) |
| 79 | + /// // or |
| 80 | + /// let oneYear: DateUnit = .years(1) |
| 81 | + /// |
| 82 | + /// - Parameter value: Amount of years in this `DateUnit` |
| 83 | + /// - Returns: A `DateUnit` with the defined number of years. |
| 84 | + public static func years(_ value: Int) -> Self { |
| 85 | + .init(calendarComponent: .year, value: value) |
| 86 | + } |
44 | 87 | }
|
45 | 88 |
|
| 89 | +/// Supports addition and subtraction of `Temporal.Date` and `Temporal.DateTime` with `DateUnit` |
46 | 90 | public protocol DateUnitOperable {
|
47 |
| - |
48 | 91 | static func + (left: Self, right: DateUnit) -> Self
|
49 |
| - |
50 | 92 | static func - (left: Self, right: DateUnit) -> Self
|
51 |
| - |
52 | 93 | }
|
53 | 94 |
|
54 | 95 | extension TemporalSpec where Self: DateUnitOperable {
|
55 | 96 |
|
| 97 | + /// Add a `DateUnit` to a `Temporal.Date` or `Temporal.DateTime` |
| 98 | + /// |
| 99 | + /// let tomorrow = Temporal.Date.now() + .days(1) |
| 100 | + /// |
| 101 | + /// - Parameters: |
| 102 | + /// - left: `Temporal.Date` or `Temporal.DateTime` |
| 103 | + /// - right: `DateUnit` to add to `left` |
| 104 | + /// - Returns: A new `Temporal.Date` or `Temporal.DateTime` the `DateUnit` was added to. |
56 | 105 | public static func + (left: Self, right: DateUnit) -> Self {
|
57 | 106 | return left.add(value: right.value, to: right.calendarComponent)
|
58 | 107 | }
|
59 | 108 |
|
| 109 | + /// Subtract a `DateUnit` from a `Temporal.Date` or `Temporal.DateTime` |
| 110 | + /// |
| 111 | + /// let yesterday = Temporal.Date.now() - .day(1) |
| 112 | + /// |
| 113 | + /// - Parameters: |
| 114 | + /// - left: `Temporal.Date` or `Temporal.DateTime` |
| 115 | + /// - right: `DateUnit` to subtract from `left` |
| 116 | + /// - Returns: A new `Temporal.Date` or `Temporal.DateTime` the `DateUnit` was subtracted from. |
60 | 117 | public static func - (left: Self, right: DateUnit) -> Self {
|
61 | 118 | return left.add(value: -right.value, to: right.calendarComponent)
|
62 | 119 | }
|
63 |
| - |
64 | 120 | }
|
0 commit comments