You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Sources/HandySwift/Types/GregorianDay.swift
+106-4Lines changed: 106 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -6,19 +6,19 @@ import Foundation
6
6
/// ```swift
7
7
/// let yesterday = GregorianDay.yesterday
8
8
/// print(yesterday.iso8601Formatted) // Prints the current date in ISO 8601 format, e.g. "2024-03-20"
9
-
///
9
+
///
10
10
/// let tomorrow = yesterday.advanced(by: 2)
11
11
/// let timCookBirthday = GregorianDay(year: 1960, month: 11, day: 01)
12
12
///
13
13
/// let startOfDay = GregorianDay(date: Date()).startOfDay()
14
14
/// ```
15
15
publicstructGregorianDay{
16
16
/// The year component of the date.
17
-
publicletyear:Int
17
+
publicvaryear:Int
18
18
/// The month component of the date.
19
-
publicletmonth:Int
19
+
publicvarmonth:Int
20
20
/// The day component of the date.
21
-
publicletday:Int
21
+
publicvarday:Int
22
22
23
23
/// Returns an ISO 8601 formatted String representation of the date, e.g., `2024-02-24`.
24
24
publicvariso8601Formatted:String{
@@ -76,6 +76,66 @@ public struct GregorianDay {
76
76
self.advanced(by:-days)
77
77
}
78
78
79
+
/// Advances the date by the specified number of months.
80
+
///
81
+
/// - Parameter months: The number of months to advance the date by.
82
+
/// - Returns: A new `GregorianDay` instance advanced by the specified number of months.
83
+
///
84
+
/// - Warning: This may return an invalid date such as February 31st. Only use in combination with a method like ``startOfMonth(timeZone:)`` that removes the day.
85
+
///
86
+
/// Example:
87
+
/// ```swift
88
+
/// let tomorrow = GregorianDay.today.advanced(byMonths: 1)
/// Reverses the date by the specified number of months.
96
+
///
97
+
/// - Parameter months: The number of months to reverse the date by.
98
+
/// - Returns: A new `GregorianDay` instance reversed by the specified number of months.
99
+
///
100
+
/// - Warning: This may return an invalid date such as February 31st. Only use in combination with a method like ``startOfMonth(timeZone:)`` that removes the day.
101
+
///
102
+
/// Example:
103
+
/// ```swift
104
+
/// let yesterday = GregorianDay.today.reversed(byMonths: 1)
105
+
/// ```
106
+
publicfunc reversed(byMonths months:Int)->Self{
107
+
self.advanced(byMonths:-months)
108
+
}
109
+
110
+
/// Advances the date by the specified number of years.
111
+
///
112
+
/// - Parameter years: The number of years to advance the date by.
113
+
/// - Returns: A new `GregorianDay` instance advanced by the specified number of years. The day and month stay the same.
114
+
///
115
+
/// - Warning: This may return an invalid date such as February 31st. Only use in combination with a method like ``startOfMonth(timeZone:)`` that removes the day.
116
+
///
117
+
/// Example:
118
+
/// ```swift
119
+
/// let tomorrow = GregorianDay.today.advanced(byYears: 1)
120
+
/// ```
121
+
publicfunc advanced(byYears years:Int)->Self{
122
+
self.with{ $0.year += years }
123
+
}
124
+
125
+
/// Reverses the date by the specified number of years.
126
+
///
127
+
/// - Parameter years: The number of years to reverse the date by.
128
+
/// - Returns: A new `GregorianDay` instance reversed by the specified number of years. The day and month stay the same.
129
+
///
130
+
/// - Warning: This may return an invalid date such as February 31st. Only use in combination with a method like ``startOfMonth(timeZone:)`` that removes the day.
131
+
/// Example:
132
+
/// ```swift
133
+
/// let yesterday = GregorianDay.today.reversed(byYears: 1)
134
+
/// ```
135
+
publicfunc reversed(byYears years:Int)->Self{
136
+
self.advanced(byYears:-years)
137
+
}
138
+
79
139
/// Returns the start of the day represented by the date.
80
140
///
81
141
/// - Parameter timeZone: The time zone for which to calculate the start of the day. Defaults to the users current timezone.
@@ -96,6 +156,46 @@ public struct GregorianDay {
96
156
return components.date!
97
157
}
98
158
159
+
/// Returns the start of the month represented by the date.
160
+
///
161
+
/// - Parameter timeZone: The time zone for which to calculate the start of the month. Defaults to the users current timezone.
162
+
/// - Returns: A `Date` representing the start of the month.
163
+
///
164
+
/// Example:
165
+
/// ```swift
166
+
/// let startOfThisMonth = GregorianDay.today.startOfMonth()
0 commit comments