Skip to content

Commit

Permalink
Merge branch 'release/6.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed May 11, 2019
2 parents a462ce3 + 54e6c37 commit 89774a1
Show file tree
Hide file tree
Showing 297 changed files with 26,250 additions and 32,117 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: objective-c
osx_image: xcode10.1
osx_image: xcode10.2

env:
global:
Expand All @@ -11,8 +11,8 @@ env:
- TVOS_FRAMEWORK_SCHEME="SwiftDate-tvOS"
- WATCHOS_FRAMEWORK_SCHEME="SwiftDate-watchOS"
matrix:
- DESTINATION="OS=11.3,name=iPhone X" SCHEME="$IOS_FRAMEWORK_SCHEME"
- DESTINATION="OS=11.3,name=Apple TV 4K" SCHEME="$TVOS_FRAMEWORK_SCHEME"
- DESTINATION="OS=12.2,name=iPhone X" SCHEME="$IOS_FRAMEWORK_SCHEME"
- DESTINATION="OS=12.2,name=Apple TV 4K" SCHEME="$TVOS_FRAMEWORK_SCHEME"
- DESTINATION="arch=x86_64" SCHEME="$MACOS_FRAMEWORK_SCHEME"

script:
Expand Down
3 changes: 2 additions & 1 deletion Documentation/0.Informations.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](./SwiftDate.png)
![](./SwiftDateArt.png)

<a name="introduction"/>

Expand Down Expand Up @@ -34,6 +34,7 @@ If you ever find a test case that is incomplete, please open an issue so we can
<a name="futureplans"/>

<a name="linux"/>

## Linux Compatibility

Since SwiftDate 5.0.13 the package compile successfully on Linux environment with the latest Swift 4.2 snapshot.
Expand Down
26 changes: 26 additions & 0 deletions Documentation/3.Manipulate_Date.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
- [3.11 - Enumerate Dates for Weekday in Range](3.Manipulate_Date.md#enumerateweekdays)
- [3.12 - Random Dates](3.Manipulate_Date.md#randomdates)
- [3.13 - Sort Dates](3.Manipulate_Date.md#sort)
- [3.14 - Get the next weekday](3.Manipulate_Date.md#nextWeekDay)
- [3.15 - Get date at given week number/weekday](3.Manipulate_Date.md#dateAtWeeknumberWeekday)

Dates can be manipulated as you need by using classic math operators and readable time units.

Expand Down Expand Up @@ -479,4 +481,28 @@ let oldestDate = DateInRegion.oldestIn(list: arrayOfDates)

[^ Top](#index)

<a name="nextWeekDay"/>

## 3.14 - Get the next weekday

In order to get the next weekday preserving smaller components (hour, minute, seconds) you can use the `nextWeekday()` function:

```swift
let date1 = DateInRegion("2019-05-11 00:00:00", format: dateFormat, region: regionRome)!
let nextFriday = date1.nextWeekday(.friday) // 2019-05-17T00:00:00+02:00
```

[^ Top](#index)

<a name="dateAtWeeknumberWeekday"/>

## 3.15 - Get date at given week number/weekday

To returns the date at the given week number and week day preserving smaller components (hour, minute, seconds) you can use the `dateAt(weekdayOrdinal:weekday:monthNumber:yearNumber:)` function:

```swift
let date = DateInRegion("2019-05-11 00:00:00", format: dateFormat, region: regionRome)!
let _ = date1.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: date1.month + 1) // 2019-06-21T00:00:00+02:00
```

[**Next Chapter**: Compare Dates](#4.CompareDates.md)
2 changes: 2 additions & 0 deletions Documentation/Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ The following documentation explore all the major features of the library. If yo
- [3.11 - Enumerate Dates for Weekday in Range](3.Manipulate_Date.md#enumerateweekdays)
- [3.12 - Random Dates](3.Manipulate_Date.md#randomdates)
- [3.13 - Sort Dates](3.Manipulate_Date.md#sort)
- [3.14 - Get the next weekday](3.Manipulate_Date.md#nextWeekDay)
- [3.15 - Get date at given week number/weekday](3.Manipulate_Date.md#dateAtWeeknumberWeekday)

### [4 - Compare Dates](4.Compare_Dates.md)

Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/Date/Date+Compare.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Date+Compare.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 07/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/Date/Date+Components.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Date+Components.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 07/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
41 changes: 37 additions & 4 deletions Sources/SwiftDate/Date/Date+Create.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Date+Operations.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand All @@ -22,7 +26,7 @@ public extension Date {
})
}

/// Return the oldest date in given list.
/// Return the newest date in given list.
///
/// - Parameter list: list of dates
/// - Returns: a tuple with the index of the oldest date and its instance.
Expand Down Expand Up @@ -214,4 +218,33 @@ public extension Date {
return DateInRegion.datesForWeekday(weekday, from: fromDate, to: toDate, region: region).map { $0.date }
}

/// Returns the date at the given week number and week day preserving smaller components (hour, minute, seconds)
///
/// For example: to get the third friday of next month
/// let today = DateInRegion()
/// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
///
/// - Parameters:
/// - weekdayOrdinal: the week number (by set position in a recurrence rule)
/// - weekday: WeekDay
/// - monthNumber: a number from 1 to 12 representing the month, optional parameter
/// - yearNumber: a number representing the year, optional parameter
/// - Returns: new date created with the given parameters
func dateAt(weekdayOrdinal: Int, weekday: WeekDay, monthNumber: Int? = nil,
yearNumber: Int? = nil) -> Date {
let date = DateInRegion(self, region: region)
return date.dateAt(weekdayOrdinal: weekdayOrdinal, weekday: weekday, monthNumber: monthNumber, yearNumber: yearNumber).date
}

/// Returns the next weekday preserving smaller components (hour, minute, seconds)
///
/// - Parameters:
/// - weekday: weekday to get.
/// - region: region target, omit to use `SwiftDate.defaultRegion`
/// - Returns: `Date`
func nextWeekday(_ weekday: WeekDay, region: Region = SwiftDate.defaultRegion) -> Date {
let date = DateInRegion(self, region: region)
return date.nextWeekday(weekday).date
}

}
10 changes: 7 additions & 3 deletions Sources/SwiftDate/Date/Date+Math.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Date+Math.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 07/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/Date/Date.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Date.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateInRegion+Compare.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 07/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateInRegion+Components.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
71 changes: 66 additions & 5 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateInRegion+Operations.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down Expand Up @@ -94,10 +98,10 @@ public extension DateInRegion {
return list.sorted(by: { $0.date.compare($1.date) == .orderedDescending })
}

/// Return the oldest date in given list (timezone is ignored, comparison uses absolute date).
/// Return the newest date in given list (timezone is ignored, comparison uses absolute date).
///
/// - Parameter list: list of dates
/// - Returns: a tuple with the index of the oldest date and its instance.
/// - Returns: a tuple with the index of the newest date and its instance.
static func newestIn(list: [DateInRegion]) -> DateInRegion? {
guard list.count > 0 else { return nil }
guard list.count > 1 else { return list.first! }
Expand Down Expand Up @@ -527,3 +531,60 @@ public extension DateInRegion {
}

}

public extension DateInRegion {

/// Returns the date at the given week number and week day preserving smaller components (hour, minute, seconds)
///
/// For example: to get the third friday of next month
/// let today = DateInRegion()
/// let result = today.dateAt(weekdayOrdinal: 3, weekday: .friday, monthNumber: today.month + 1)
///
/// - Parameters:
/// - weekdayOrdinal: the week number (by set position in a recurrence rule)
/// - weekday: WeekDay
/// - monthNumber: a number from 1 to 12 representing the month, optional parameter
/// - yearNumber: a number representing the year, optional parameter
/// - Returns: new date created with the given parameters
func dateAt(weekdayOrdinal: Int, weekday: WeekDay, monthNumber: Int? = nil,
yearNumber: Int? = nil) -> DateInRegion {
let monthNum = monthNumber ?? month
let yearNum = yearNumber ?? year

var requiredWeekNum = weekdayOrdinal
var result = DateInRegion(year: yearNum, month: monthNum, day: 1, hour: hour,
minute: minute, second: second, nanosecond: nanosecond, region: region)

if result.weekday == weekday.rawValue {
requiredWeekNum -= 1
}

while requiredWeekNum > 0 {
result = result.nextWeekday(weekday)
requiredWeekNum -= 1
}

return result
}

/// Returns the next weekday preserving smaller components (hour, minute, seconds)
///
/// - Parameters:
/// - weekday: weekday to get.
/// - region: region target, omit to use `SwiftDate.defaultRegion`
/// - Returns: `DateInRegion`
func nextWeekday(_ weekday: WeekDay) -> DateInRegion {
var components = DateComponents()
components.weekday = weekday.rawValue
components.hour = hour
components.second = second
components.minute = minute

guard let next = region.calendar.nextDate(after: date, matching: components,
matchingPolicy: .nextTimePreservingSmallerComponents) else {
return self
}

return DateInRegion(next, region: region)
}
}
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Math.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateInRegion+Math.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 07/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateInRegion/DateInRegion.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateInRegion.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateInRegion/Region.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// Region.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
10 changes: 7 additions & 3 deletions Sources/SwiftDate/DateRepresentable.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
//
// DateRepresentable.swift
// SwiftDate
// Parse, validate, manipulate, and display dates, time and timezones in Swift
//
// Created by Daniele Margutti on 06/06/2018.
// Copyright © 2018 SwiftDate. All rights reserved.
// Created by Daniele Margutti
// - Web: https://www.danielemargutti.com
// - Twitter: https://twitter.com/danielemargutti
// - Mail: [email protected]
//
// Copyright © 2019 Daniele Margutti. Licensed under MIT License.
//

import Foundation
Expand Down
Loading

0 comments on commit 89774a1

Please sign in to comment.