Skip to content

Commit 04c7abf

Browse files
committed
Changes from SwiftDate/malcommac#782
1 parent d8bf0f2 commit 04c7abf

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

Sources/SwiftDate/Date/Date+Create.swift

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,21 @@ public extension Date {
105105
/// - parameter unit: A TimeUnit value.
106106
///
107107
/// - returns: A new Moment instance.
108+
///
108109
func dateAtEndOf(_ unit: Calendar.Component) -> Date {
109-
inDefaultRegion().dateAtEndOf(unit).date
110+
switch unit {
111+
case .day: return inDefaultRegion().dateAt(.endOfDay).date
112+
case .month: return inDefaultRegion().dateAt(.endOfMonth).date
113+
default: return inDefaultRegion().dateAtEndOf(unit).date
114+
}
110115
}
111116

112-
/// Return a new DateInRegion that is initialized at the end of the specified components
113-
/// executed in order.
117+
/// Return a new ``DateInRegion`` that is initialized at the end of the
118+
/// specified components executed in order.
114119
///
115120
/// - Parameter units: sequence of transformations as time unit components
116121
/// - Returns: new date at the end of the passed components, intermediate results if fails.
122+
///
117123
func dateAtEndOf(_ units: [Calendar.Component]) -> Date {
118124
units.reduce(self) { (currentDate, currentUnit) -> Date in
119125
return currentDate.dateAtEndOf(currentUnit)
@@ -136,7 +142,7 @@ public extension Date {
136142
/// - secs: sec to set (`nil` to leave it unaltered)
137143
/// - ms: milliseconds to set (`nil` to leave it unaltered)
138144
/// - options: options for calculation
139-
/// - Returns: new altered `DateInRegion` instance
145+
/// - Returns: new altered ``DateInRegion`` instance
140146
func dateBySet(hour: Int?, min: Int?, secs: Int?, ms: Int? = nil, options: TimeCalculationOptions = TimeCalculationOptions()) -> Date? {
141147
let srcDate = DateInRegion(self, region: SwiftDate.defaultRegion)
142148
return srcDate.dateBySet(hour: hour, min: min, secs: secs, ms: ms, options: options)?.date
@@ -165,6 +171,7 @@ public extension Date {
165171
/// - count: value of the offset.
166172
/// - component: component to offset.
167173
/// - Returns: new altered date.
174+
///
168175
func dateByAdding(_ count: Int, _ component: Calendar.Component) -> DateInRegion {
169176
DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component)
170177
}
@@ -203,33 +210,43 @@ public extension Date {
203210
}
204211

205212
/// Return the dates for a specific weekday inside a specified date range.
206-
/// NOTE: Values are returned in order.
207213
///
214+
/// - note: Values are returned in order.
215+
///
208216
/// - Parameters:
209217
/// - weekday: weekday target.
210218
/// - startDate: from date of the range.
211219
/// - endDate: to date of the range.
212220
/// - region: region target, omit to use `SwiftDate.defaultRegion`
213221
/// - Returns: Ordered list of the dates for given weekday in passed range.
214-
static func datesForWeekday(_ weekday: WeekDay, from startDate: Date, to endDate: Date,
215-
region: Region = SwiftDate.defaultRegion) -> [Date] {
222+
///
223+
static func datesForWeekday(
224+
_ weekday: WeekDay,
225+
from startDate: Date,
226+
to endDate: Date,
227+
region: Region = SwiftDate.defaultRegion
228+
) -> [Date] {
216229
let fromDate = DateInRegion(startDate, region: region)
217230
let toDate = DateInRegion(endDate, region: region)
218231
return DateInRegion.datesForWeekday(weekday, from: fromDate, to: toDate, region: region).map { $0.date }
219232
}
220233

221-
/// Returns the date at the given week number and week day preserving smaller components (hour, minute, seconds)
234+
/// Returns the date at the given week number and week day preserving smaller
235+
/// components (hour, minute, seconds)
222236
///
223-
/// For example: to get the third friday of next month
224-
/// let today = DateInRegion()
225-
/// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
237+
/// For example, to get the third friday of next month:
238+
/// ```swift
239+
/// let today = DateInRegion()
240+
/// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
241+
///```
226242
///
227243
/// - Parameters:
228244
/// - weekdayOrdinal: the week number (by set position in a recurrence rule)
229245
/// - weekday: WeekDay
230246
/// - monthNumber: a number from 1 to 12 representing the month, optional parameter
231247
/// - yearNumber: a number representing the year, optional parameter
232248
/// - Returns: new date created with the given parameters
249+
///
233250
func dateAt(weekdayOrdinal: Int, weekday: WeekDay, monthNumber: Int? = nil,
234251
yearNumber: Int? = nil) -> Date {
235252
let date = DateInRegion(self, region: region)
@@ -240,8 +257,9 @@ public extension Date {
240257
///
241258
/// - Parameters:
242259
/// - weekday: weekday to get.
243-
/// - region: region target, omit to use `SwiftDate.defaultRegion`
260+
/// - region: region target, omit to use ``SwiftDate/SwiftDate/defaultRegion``
244261
/// - Returns: `Date`
262+
///
245263
func nextWeekday(_ weekday: WeekDay, region: Region = SwiftDate.defaultRegion) -> Date {
246264
let date = DateInRegion(self, region: region)
247265
return date.nextWeekday(weekday).date

0 commit comments

Comments
 (0)