Skip to content

Commit

Permalink
malcommac#648 Swift5 Compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
malcommac committed Mar 27, 2019
1 parent 600a641 commit 3ddf6d5
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 199 deletions.
Binary file modified Playgrounds/SwiftDate.playground/Contents.o
Binary file not shown.
16 changes: 8 additions & 8 deletions Sources/SwiftDate/Date/Date+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public extension Date {
/// - refDate: reference date compare against to.
/// - precision: The precision of the comparison (default is 5 minutes, or 300 seconds).
/// - Returns: A boolean; true if close by, false otherwise.
public func compareCloseTo(_ refDate: Date, precision: TimeInterval = 300) -> Bool {
func compareCloseTo(_ refDate: Date, precision: TimeInterval = 300) -> Bool {
return (abs(timeIntervalSince(refDate)) < precision)
}

Expand All @@ -30,7 +30,7 @@ public extension Date {
///
/// - Parameter compareType: comparison type.
/// - Returns: `true` if comparison succeded, `false` otherwise
public func compare(_ compareType: DateComparisonType) -> Bool {
func compare(_ compareType: DateComparisonType) -> Bool {
return inDefaultRegion().compare(compareType)
}

Expand All @@ -51,7 +51,7 @@ public extension Date {
/// - orEqual: `true` to also check for equality
/// - granularity: smallest unit that must, along with all larger units, be less for the given dates
/// - Returns: Boolean
public func isBeforeDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
func isBeforeDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
return inDefaultRegion().isBeforeDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

Expand All @@ -62,7 +62,7 @@ public extension Date {
/// - orEqual: `true` to also check for equality
/// - granularity: Smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
public func isAfterDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
func isAfterDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
return inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

Expand All @@ -74,7 +74,7 @@ public extension Date {
/// - orEqual: `true` to also check for equality on date and date2
/// - granularity: smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
public func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool {
func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool {
return self.inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity)
}

Expand All @@ -86,7 +86,7 @@ public extension Date {
/// dates to be considered the same.
///
/// - returns: `true` if the dates are the same down to the given granularity, otherwise `false`
public func isInside(date: Date, granularity: Calendar.Component) -> Bool {
func isInside(date: Date, granularity: Calendar.Component) -> Bool {
return (compare(toDate: date, granularity: granularity) == .orderedSame)
}

Expand All @@ -96,15 +96,15 @@ public extension Date {
///
/// - Parameter date: The date to compare to self
/// - Returns: The date that is earlier
public func earlierDate(_ date: Date) -> Date {
func earlierDate(_ date: Date) -> Date {
return (timeIntervalSince1970 <= date.timeIntervalSince1970) ? self : date
}

/// Return the later of two dates, between self and a given date.
///
/// - Parameter date: The date to compare to self
/// - Returns: The date that is later
public func laterDate(_ date: Date) -> Date {
func laterDate(_ date: Date) -> Date {
return (timeIntervalSince1970 >= date.timeIntervalSince1970) ? self : date
}

Expand Down
10 changes: 5 additions & 5 deletions Sources/SwiftDate/Date/Date+Components.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,25 @@ import Foundation
public extension Date {

/// Indicates whether the month is a leap month.
public var isLeapMonth: Bool {
var isLeapMonth: Bool {
return inDefaultRegion().isLeapMonth
}

/// Indicates whether the year is a leap year.
public var isLeapYear: Bool {
var isLeapYear: Bool {
return inDefaultRegion().isLeapYear
}

/// Julian day is the continuous count of days since the beginning of
/// the Julian Period used primarily by astronomers.
public var julianDay: Double {
var julianDay: Double {
return inDefaultRegion().julianDay
}

/// The Modified Julian Date (MJD) was introduced by the Smithsonian Astrophysical Observatory
/// in 1957 to record the orbit of Sputnik via an IBM 704 (36-bit machine)
/// and using only 18 bits until August 7, 2576.
public var modifiedJulianDay: Double {
var modifiedJulianDay: Double {
return inDefaultRegion().modifiedJulianDay
}

Expand All @@ -39,7 +39,7 @@ public extension Date {
/// - refDate: reference date (`nil` to use current date in the same region of the receiver)
/// - component: time unit to extract.
/// - Returns: value
public func getInterval(toDate: Date?, component: Calendar.Component) -> Int64 {
func getInterval(toDate: Date?, component: Calendar.Component) -> Int64 {
return inDefaultRegion().getInterval(toDate: toDate?.inDefaultRegion(), component: component)
}
}
36 changes: 18 additions & 18 deletions Sources/SwiftDate/Date/Date+Create.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public extension Date {
///
/// - Parameter list: list of dates
/// - Returns: a tuple with the index of the oldest date and its instance.
public static func oldestIn(list: [Date]) -> Date? {
static func oldestIn(list: [Date]) -> Date? {
guard list.count > 0 else { return nil }
guard list.count > 1 else { return list.first! }
return list.min(by: {
Expand All @@ -26,7 +26,7 @@ public extension Date {
///
/// - Parameter list: list of dates
/// - Returns: a tuple with the index of the oldest date and its instance.
public static func newestIn(list: [Date]) -> Date? {
static func newestIn(list: [Date]) -> Date? {
guard list.count > 0 else { return nil }
guard list.count > 1 else { return list.first! }
return list.max(by: {
Expand All @@ -43,7 +43,7 @@ public extension Date {
/// - endDate: ending date
/// - increment: increment function. It get the last generated date and require a valida `DateComponents` instance which define the increment
/// - Returns: array of dates
public static func enumerateDates(from startDate: Date, to endDate: Date, increment: ((Date) -> (DateComponents))) -> [Date] {
static func enumerateDates(from startDate: Date, to endDate: Date, increment: ((Date) -> (DateComponents))) -> [Date] {
var dates: [Date] = []
var currentDate = startDate

Expand All @@ -63,7 +63,7 @@ public extension Date {
/// - endDate: ending date
/// - increment: components to add
/// - Returns: array of dates
public static func enumerateDates(from startDate: Date, to endDate: Date, increment: DateComponents) -> [Date] {
static func enumerateDates(from startDate: Date, to endDate: Date, increment: DateComponents) -> [Date] {
return Date.enumerateDates(from: startDate, to: endDate, increment: { _ in
return increment
})
Expand All @@ -73,15 +73,15 @@ public extension Date {
///
/// - Parameter style: rounding mode.
/// - Returns: rounded date
public func dateRoundedAt(at style: RoundDateMode) -> Date {
func dateRoundedAt(at style: RoundDateMode) -> Date {
return inDefaultRegion().dateRoundedAt(style).date
}

/// Returns a new DateInRegion that is initialized at the start of a specified unit of time.
///
/// - Parameter unit: time unit value.
/// - Returns: instance at the beginning of the time unit; `self` if fails.
public func dateAtStartOf(_ unit: Calendar.Component) -> Date {
func dateAtStartOf(_ unit: Calendar.Component) -> Date {
return inDefaultRegion().dateAtStartOf(unit).date
}

Expand All @@ -90,7 +90,7 @@ public extension Date {
///
/// - Parameter units: sequence of transformations as time unit components
/// - Returns: new date at the beginning of the passed components, intermediate results if fails.
public func dateAtStartOf(_ units: [Calendar.Component]) -> Date {
func dateAtStartOf(_ units: [Calendar.Component]) -> Date {
return units.reduce(self) { (currentDate, currentUnit) -> Date in
return currentDate.dateAtStartOf(currentUnit)
}
Expand All @@ -101,7 +101,7 @@ public extension Date {
/// - parameter unit: A TimeUnit value.
///
/// - returns: A new Moment instance.
public func dateAtEndOf(_ unit: Calendar.Component) -> Date {
func dateAtEndOf(_ unit: Calendar.Component) -> Date {
return inDefaultRegion().dateAtEndOf(unit).date
}

Expand All @@ -110,7 +110,7 @@ public extension Date {
///
/// - Parameter units: sequence of transformations as time unit components
/// - Returns: new date at the end of the passed components, intermediate results if fails.
public func dateAtEndOf(_ units: [Calendar.Component]) -> Date {
func dateAtEndOf(_ units: [Calendar.Component]) -> Date {
return units.reduce(self) { (currentDate, currentUnit) -> Date in
return currentDate.dateAtEndOf(currentUnit)
}
Expand All @@ -120,7 +120,7 @@ public extension Date {
///
/// - Parameter components: components to alter with their new values.
/// - Returns: new altered `DateInRegion` instance
public func dateBySet(_ components: [Calendar.Component: Int]) -> Date? {
func dateBySet(_ components: [Calendar.Component: Int]) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateBySet(components)?.date
}

Expand All @@ -133,7 +133,7 @@ public extension Date {
/// - ms: milliseconds to set (`nil` to leave it unaltered)
/// - options: options for calculation
/// - Returns: new altered `DateInRegion` instance
public func dateBySet(hour: Int?, min: Int?, secs: Int?, ms: Int? = nil, options: TimeCalculationOptions = TimeCalculationOptions()) -> Date? {
func dateBySet(hour: Int?, min: Int?, secs: Int?, ms: Int? = nil, options: TimeCalculationOptions = TimeCalculationOptions()) -> Date? {
let srcDate = DateInRegion(self, region: SwiftDate.defaultRegion)
return srcDate.dateBySet(hour: hour, min: min, secs: secs, ms: ms, options: options)?.date
}
Expand All @@ -142,15 +142,15 @@ public extension Date {
///
/// - Parameter components: components to truncate.
/// - Returns: new date with truncated components.
public func dateTruncated(_ components: [Calendar.Component]) -> Date? {
func dateTruncated(_ components: [Calendar.Component]) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(at: components)?.date
}

/// Creates a new instance by truncating the components starting from given components down the granurality.
///
/// - Parameter component: The component to be truncated from.
/// - Returns: new date with truncated components.
public func dateTruncated(from component: Calendar.Component) -> Date? {
func dateTruncated(from component: Calendar.Component) -> Date? {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(from: component)?.date
}

Expand All @@ -161,23 +161,23 @@ public extension Date {
/// - count: value of the offset.
/// - component: component to offset.
/// - Returns: new altered date.
public func dateByAdding(_ count: Int, _ component: Calendar.Component) -> DateInRegion {
func dateByAdding(_ count: Int, _ component: Calendar.Component) -> DateInRegion {
return DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component)
}

/// Return related date starting from the receiver attributes.
///
/// - Parameter type: related date to obtain.
/// - Returns: instance of the related date.
public func dateAt(_ type: DateRelatedType) -> Date {
func dateAt(_ type: DateRelatedType) -> Date {
return inDefaultRegion().dateAt(type).date
}

/// Create a new date at now and extract the related date using passed rule type.
///
/// - Parameter type: related date to obtain.
/// - Returns: instance of the related date.
public static func nowAt(_ type: DateRelatedType) -> Date {
static func nowAt(_ type: DateRelatedType) -> Date {
return Date().dateAt(type)
}

Expand All @@ -191,7 +191,7 @@ public extension Date {
/// - year: year target.
/// - region: region target, omit to use `SwiftDate.defaultRegion`
/// - Returns: Ordered list of the dates for given weekday into given month.
public static func datesForWeekday(_ weekday: WeekDay, inMonth month: Int, ofYear year: Int,
static func datesForWeekday(_ weekday: WeekDay, inMonth month: Int, ofYear year: Int,
region: Region = SwiftDate.defaultRegion) -> [Date] {
let fromDate = DateInRegion(Date(year: year, month: month, day: 1, hour: 0, minute: 0), region: region)
let toDate = fromDate.dateAt(.endOfMonth)
Expand All @@ -207,7 +207,7 @@ public extension Date {
/// - endDate: to date of the range.
/// - region: region target, omit to use `SwiftDate.defaultRegion`
/// - Returns: Ordered list of the dates for given weekday in passed range.
public static func datesForWeekday(_ weekday: WeekDay, from startDate: Date, to endDate: Date,
static func datesForWeekday(_ weekday: WeekDay, from startDate: Date, to endDate: Date,
region: Region = SwiftDate.defaultRegion) -> [Date] {
let fromDate = DateInRegion(startDate, region: region)
let toDate = DateInRegion(endDate, region: region)
Expand Down
16 changes: 8 additions & 8 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ public extension DateInRegion {
/// - refDate: reference date compare against to.
/// - precision: The precision of the comparison (default is 5 minutes, or 300 seconds).
/// - Returns: A boolean; true if close by, false otherwise.
public func compareCloseTo(_ refDate: DateInRegion, precision: TimeInterval = 300) -> Bool {
func compareCloseTo(_ refDate: DateInRegion, precision: TimeInterval = 300) -> Bool {
return (abs(date.timeIntervalSince(refDate.date)) <= precision)
}

/// Compare the date with the rule specified in the `compareType` parameter.
///
/// - Parameter compareType: comparison type.
/// - Returns: `true` if comparison succeded, `false` otherwise
public func compare(_ compareType: DateComparisonType) -> Bool {
func compare(_ compareType: DateComparisonType) -> Bool {
switch compareType {
case .isToday:
return compare(.isSameDay(region.nowInThisRegion()))
Expand Down Expand Up @@ -247,7 +247,7 @@ public extension DateInRegion {
/// - orEqual: `true` to also check for equality
/// - granularity: smallest unit that must, along with all larger units, be less for the given dates
/// - Returns: Boolean
public func isBeforeDate(_ date: DateInRegion, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
func isBeforeDate(_ date: DateInRegion, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
let result = compare(toDate: date, granularity: granularity)
return (orEqual ? (result == .orderedSame || result == .orderedAscending) : result == .orderedAscending)
}
Expand All @@ -259,7 +259,7 @@ public extension DateInRegion {
/// - orEqual: `true` to also check for equality
/// - granularity: Smallest unit that must, along with all larger units, be greater for the given dates.
/// - Returns: Boolean
public func isAfterDate(_ refDate: DateInRegion, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
func isAfterDate(_ refDate: DateInRegion, orEqual: Bool = false, granularity: Calendar.Component) -> Bool {
let result = compare(toDate: refDate, granularity: granularity)
return (orEqual ? (result == .orderedSame || result == .orderedDescending) : result == .orderedDescending)
}
Expand All @@ -272,7 +272,7 @@ public extension DateInRegion {
/// dates to be considered the same.
///
/// - returns: `true` if the dates are the same down to the given granularity, otherwise `false`
public func isInside(date: DateInRegion, granularity: Calendar.Component) -> Bool {
func isInside(date: DateInRegion, granularity: Calendar.Component) -> Bool {
return (compare(toDate: date, granularity: granularity) == .orderedSame)
}

Expand All @@ -284,7 +284,7 @@ public extension DateInRegion {
/// - orEqual: `true` to also check for equality on date and date2, default is `true`
/// - granularity: smallest unit that must, along with all larger units, be greater
/// - Returns: Boolean
public func isInRange(date startDate: DateInRegion, and endDate: DateInRegion, orEqual: Bool = true, granularity: Calendar.Component = .nanosecond) -> Bool {
func isInRange(date startDate: DateInRegion, and endDate: DateInRegion, orEqual: Bool = true, granularity: Calendar.Component = .nanosecond) -> Bool {
return isAfterDate(startDate, orEqual: orEqual, granularity: granularity) && isBeforeDate(endDate, orEqual: orEqual, granularity: granularity)
}

Expand All @@ -294,15 +294,15 @@ public extension DateInRegion {
///
/// - Parameter date: The date to compare to self
/// - Returns: The date that is earlier
public func earlierDate(_ date: DateInRegion) -> DateInRegion {
func earlierDate(_ date: DateInRegion) -> DateInRegion {
return (self.date.timeIntervalSince1970 <= date.date.timeIntervalSince1970) ? self : date
}

/// Return the later of two dates, between self and a given date.
///
/// - Parameter date: The date to compare to self
/// - Returns: The date that is later
public func laterDate(_ date: DateInRegion) -> DateInRegion {
func laterDate(_ date: DateInRegion) -> DateInRegion {
return (self.date.timeIntervalSince1970 >= date.date.timeIntervalSince1970) ? self : date
}

Expand Down
Loading

0 comments on commit 3ddf6d5

Please sign in to comment.