-
-
Notifications
You must be signed in to change notification settings - Fork 770
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
297 changed files
with
26,250 additions
and
32,117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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. | ||
|
@@ -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 | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
10 changes: 7 additions & 3 deletions
10
Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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! } | ||
|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
Oops, something went wrong.