From 4dc40e7b7945f253b085f9327c738209d08424cb Mon Sep 17 00:00:00 2001 From: Daniele Margutti Date: Sat, 10 Sep 2022 17:13:03 +0200 Subject: [PATCH] Cleanup of return for single line statements --- Sources/SwiftDate/Date/Date+Compare.swift | 20 ++++++------- Sources/SwiftDate/Date/Date+Components.swift | 10 +++---- Sources/SwiftDate/Date/Date+Create.swift | 24 ++++++++-------- Sources/SwiftDate/Date/Date+Math.swift | 10 +++---- Sources/SwiftDate/Date/Date.swift | 26 ++++------------- .../DateInRegion/DateInRegion+Compare.swift | 20 ++++++------- .../DateInRegion+Components.swift | 6 ++-- .../DateInRegion/DateInRegion+Create.swift | 28 ++++--------------- .../DateInRegion/DateInRegion+Math.swift | 4 +-- .../SwiftDate/DateInRegion/DateInRegion.swift | 4 +-- Sources/SwiftDate/DateInRegion/Region.swift | 2 +- .../Int+DateComponents.swift | 18 ++++++------ .../Foundation+Extras/String+Parser.swift | 17 +++++------ .../TimeInterval+Formatter.swift | 2 +- .../SwiftDate/Supports/AssociatedValues.swift | 10 ++----- Tests/SwiftDateTests/TestFormatters.swift | 6 ---- 16 files changed, 84 insertions(+), 123 deletions(-) diff --git a/Sources/SwiftDate/Date/Date+Compare.swift b/Sources/SwiftDate/Date/Date+Compare.swift index e5f4246e..9fc09caf 100644 --- a/Sources/SwiftDate/Date/Date+Compare.swift +++ b/Sources/SwiftDate/Date/Date+Compare.swift @@ -25,7 +25,7 @@ public extension Date { /// - precision: The precision of the comparison (default is 5 minutes, or 300 seconds). /// - Returns: A boolean; true if close by, false otherwise. func compareCloseTo(_ refDate: Date, precision: TimeInterval = 300) -> Bool { - return (abs(timeIntervalSince(refDate)) < precision) + (abs(timeIntervalSince(refDate)) < precision) } // MARK: - Extendend Compare @@ -35,7 +35,7 @@ public extension Date { /// - Parameter compareType: comparison type. /// - Returns: `true` if comparison succeded, `false` otherwise func compare(_ compareType: DateComparisonType) -> Bool { - return inDefaultRegion().compare(compareType) + inDefaultRegion().compare(compareType) } /// Returns a ComparisonResult value that indicates the ordering of two given dates based on @@ -45,7 +45,7 @@ public extension Date { /// - parameter granularity: The smallest unit that must, along with all larger units be less for the given dates /// - returns: `ComparisonResult` func compare(toDate refDate: Date, granularity: Calendar.Component) -> ComparisonResult { - return inDefaultRegion().compare(toDate: refDate.inDefaultRegion(), granularity: granularity) + inDefaultRegion().compare(toDate: refDate.inDefaultRegion(), granularity: granularity) } /// Compares whether the receiver is before/before equal `date` based on their components down to a given unit granularity. @@ -56,7 +56,7 @@ public extension Date { /// - granularity: smallest unit that must, along with all larger units, be less for the given dates /// - Returns: Boolean func isBeforeDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool { - return inDefaultRegion().isBeforeDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) + inDefaultRegion().isBeforeDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) } /// Compares whether the receiver is after `date` based on their components down to a given unit granularity. @@ -67,7 +67,7 @@ public extension Date { /// - granularity: Smallest unit that must, along with all larger units, be greater for the given dates. /// - Returns: Boolean func isAfterDate(_ refDate: Date, orEqual: Bool = false, granularity: Calendar.Component) -> Bool { - return inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) + inDefaultRegion().isAfterDate(refDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) } /// Returns a value between 0.0 and 1.0 or nil, that is the position of current date between 2 other dates. @@ -77,7 +77,7 @@ public extension Date { /// - endDate: range lower bound date /// - Returns: `nil` if current date is not between `startDate` and `endDate`. Otherwise returns position between `startDate` and `endDate`. func positionInRange(date startDate: Date, and endDate: Date) -> Double? { - return inDefaultRegion().positionInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion()) + inDefaultRegion().positionInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion()) } /// Return true if receiver date is contained in the range specified by two dates. @@ -89,7 +89,7 @@ public extension Date { /// - granularity: smallest unit that must, along with all larger units, be greater for the given dates. /// - Returns: Boolean func isInRange(date startDate: Date, and endDate: Date, orEqual: Bool = false, granularity: Calendar.Component = .nanosecond) -> Bool { - return inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) + inDefaultRegion().isInRange(date: startDate.inDefaultRegion(), and: endDate.inDefaultRegion(), orEqual: orEqual, granularity: granularity) } /// Compares equality of two given dates based on their components down to a given unit @@ -101,7 +101,7 @@ public extension Date { /// /// - returns: `true` if the dates are the same down to the given granularity, otherwise `false` func isInside(date: Date, granularity: Calendar.Component) -> Bool { - return (compare(toDate: date, granularity: granularity) == .orderedSame) + (compare(toDate: date, granularity: granularity) == .orderedSame) } // MARK: - Date Earlier/Later @@ -111,7 +111,7 @@ public extension Date { /// - Parameter date: The date to compare to self /// - Returns: The date that is earlier func earlierDate(_ date: Date) -> Date { - return timeIntervalSince(date) <= 0 ? self : date + timeIntervalSince(date) <= 0 ? self : date } /// Return the later of two dates, between self and a given date. @@ -119,7 +119,7 @@ public extension Date { /// - Parameter date: The date to compare to self /// - Returns: The date that is later func laterDate(_ date: Date) -> Date { - return timeIntervalSince(date) >= 0 ? self : date + timeIntervalSince(date) >= 0 ? self : date } } diff --git a/Sources/SwiftDate/Date/Date+Components.swift b/Sources/SwiftDate/Date/Date+Components.swift index 137a4c87..4171bb42 100644 --- a/Sources/SwiftDate/Date/Date+Components.swift +++ b/Sources/SwiftDate/Date/Date+Components.swift @@ -16,25 +16,25 @@ public extension Date { /// Indicates whether the month is a leap month. var isLeapMonth: Bool { - return inDefaultRegion().isLeapMonth + inDefaultRegion().isLeapMonth } /// Indicates whether the year is a leap year. var isLeapYear: Bool { - return inDefaultRegion().isLeapYear + inDefaultRegion().isLeapYear } /// Julian day is the continuous count of days since the beginning of /// the Julian Period used primarily by astronomers. var julianDay: Double { - return inDefaultRegion().julianDay + 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. var modifiedJulianDay: Double { - return inDefaultRegion().modifiedJulianDay + inDefaultRegion().modifiedJulianDay } /// Return elapsed time expressed in given components since the current receiver and a reference date. @@ -44,6 +44,6 @@ public extension Date { /// - component: time unit to extract. /// - Returns: value func getInterval(toDate: Date?, component: Calendar.Component) -> Int64 { - return inDefaultRegion().getInterval(toDate: toDate?.inDefaultRegion(), component: component) + inDefaultRegion().getInterval(toDate: toDate?.inDefaultRegion(), component: component) } } diff --git a/Sources/SwiftDate/Date/Date+Create.swift b/Sources/SwiftDate/Date/Date+Create.swift index 965f5c35..e54b6c53 100644 --- a/Sources/SwiftDate/Date/Date+Create.swift +++ b/Sources/SwiftDate/Date/Date+Create.swift @@ -68,7 +68,7 @@ public extension Date { /// - increment: components to add /// - Returns: array of dates static func enumerateDates(from startDate: Date, to endDate: Date, increment: DateComponents) -> [Date] { - return Date.enumerateDates(from: startDate, to: endDate, increment: { _ in + Date.enumerateDates(from: startDate, to: endDate, increment: { _ in return increment }) } @@ -78,7 +78,7 @@ public extension Date { /// - Parameter style: rounding mode. /// - Returns: rounded date func dateRoundedAt(at style: RoundDateMode) -> Date { - return inDefaultRegion().dateRoundedAt(style).date + inDefaultRegion().dateRoundedAt(style).date } /// Returns a new DateInRegion that is initialized at the start of a specified unit of time. @@ -86,7 +86,7 @@ public extension Date { /// - Parameter unit: time unit value. /// - Returns: instance at the beginning of the time unit; `self` if fails. func dateAtStartOf(_ unit: Calendar.Component) -> Date { - return inDefaultRegion().dateAtStartOf(unit).date + inDefaultRegion().dateAtStartOf(unit).date } /// Return a new DateInRegion that is initialized at the start of the specified components @@ -95,7 +95,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. func dateAtStartOf(_ units: [Calendar.Component]) -> Date { - return units.reduce(self) { (currentDate, currentUnit) -> Date in + units.reduce(self) { (currentDate, currentUnit) -> Date in return currentDate.dateAtStartOf(currentUnit) } } @@ -106,7 +106,7 @@ public extension Date { /// /// - returns: A new Moment instance. func dateAtEndOf(_ unit: Calendar.Component) -> Date { - return inDefaultRegion().dateAtEndOf(unit).date + inDefaultRegion().dateAtEndOf(unit).date } /// Return a new DateInRegion that is initialized at the end of the specified components @@ -115,7 +115,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. func dateAtEndOf(_ units: [Calendar.Component]) -> Date { - return units.reduce(self) { (currentDate, currentUnit) -> Date in + units.reduce(self) { (currentDate, currentUnit) -> Date in return currentDate.dateAtEndOf(currentUnit) } } @@ -125,7 +125,7 @@ public extension Date { /// - Parameter components: components to alter with their new values. /// - Returns: new altered `DateInRegion` instance func dateBySet(_ components: [Calendar.Component: Int]) -> Date? { - return DateInRegion(self, region: SwiftDate.defaultRegion).dateBySet(components)?.date + DateInRegion(self, region: SwiftDate.defaultRegion).dateBySet(components)?.date } /// Create a new date by altering specified time components. @@ -147,7 +147,7 @@ public extension Date { /// - Parameter components: components to truncate. /// - Returns: new date with truncated components. func dateTruncated(_ components: [Calendar.Component]) -> Date? { - return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(at: components)?.date + DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(at: components)?.date } /// Creates a new instance by truncating the components starting from given components down the granurality. @@ -155,7 +155,7 @@ public extension Date { /// - Parameter component: The component to be truncated from. /// - Returns: new date with truncated components. func dateTruncated(from component: Calendar.Component) -> Date? { - return DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(from: component)?.date + DateInRegion(self, region: SwiftDate.defaultRegion).dateTruncated(from: component)?.date } /// Offset a date by n calendar components. @@ -166,7 +166,7 @@ public extension Date { /// - component: component to offset. /// - Returns: new altered date. func dateByAdding(_ count: Int, _ component: Calendar.Component) -> DateInRegion { - return DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component) + DateInRegion(self, region: SwiftDate.defaultRegion).dateByAdding(count, component) } /// Return related date starting from the receiver attributes. @@ -174,7 +174,7 @@ public extension Date { /// - Parameter type: related date to obtain. /// - Returns: instance of the related date. func dateAt(_ type: DateRelatedType) -> Date { - return inDefaultRegion().dateAt(type).date + inDefaultRegion().dateAt(type).date } /// Create a new date at now and extract the related date using passed rule type. @@ -182,7 +182,7 @@ public extension Date { /// - Parameter type: related date to obtain. /// - Returns: instance of the related date. static func nowAt(_ type: DateRelatedType) -> Date { - return Date().dateAt(type) + Date().dateAt(type) } /// Return the dates for a specific weekday inside given month of specified year. diff --git a/Sources/SwiftDate/Date/Date+Math.swift b/Sources/SwiftDate/Date/Date+Math.swift index 5383c9bd..c199ab88 100644 --- a/Sources/SwiftDate/Date/Date+Math.swift +++ b/Sources/SwiftDate/Date/Date+Math.swift @@ -17,24 +17,24 @@ import Foundation /// let difference = lhs - rhs /// rhs + difference = lhs public func - (lhs: Date, rhs: Date) -> DateComponents { - return SwiftDate.defaultRegion.calendar.dateComponents(DateComponents.allComponentsSet, from: rhs, to: lhs) + SwiftDate.defaultRegion.calendar.dateComponents(DateComponents.allComponentsSet, from: rhs, to: lhs) } /// Adds date components to a date and returns a new date. public func + (lhs: Date, rhs: DateComponents) -> Date { - return rhs.from(lhs)! + rhs.from(lhs)! } /// Adds date components to a date and returns a new date. public func + (lhs: DateComponents, rhs: Date) -> Date { - return (rhs + lhs) + (rhs + lhs) } /// Subtracts date components from a date and returns a new date. public func - (lhs: Date, rhs: DateComponents) -> Date { - return (lhs + (-rhs)) + (lhs + (-rhs)) } public func + (lhs: Date, rhs: TimeInterval) -> Date { - return lhs.addingTimeInterval(rhs) + lhs.addingTimeInterval(rhs) } diff --git a/Sources/SwiftDate/Date/Date.swift b/Sources/SwiftDate/Date/Date.swift index 9e7b8011..b77ab966 100644 --- a/Sources/SwiftDate/Date/Date.swift +++ b/Sources/SwiftDate/Date/Date.swift @@ -12,13 +12,9 @@ import Foundation -#if os(Linux) - -#else internal enum AssociatedKeys: String { case customDateFormatter = "SwiftDate.CustomDateFormatter" } -#endif extension Date: DateRepresentable { @@ -27,18 +23,9 @@ extension Date: DateRepresentable { /// For absolute Date object the default region is obtained from the global `defaultRegion` variable. public var region: Region { - return SwiftDate.defaultRegion + SwiftDate.defaultRegion } - #if os(Linux) - public var customFormatter: DateFormatter? { - get { - debugPrint("Not supported on Linux") - return nil - } - set { debugPrint("Not supported on Linux") } - } - #else /// Assign a custom formatter if you need a special behaviour during formatting of the object. /// Usually you will not need to do it, SwiftDate uses the local thread date formatter in order to /// optimize the formatting process. By default is `nil`. @@ -51,11 +38,10 @@ extension Date: DateRepresentable { set(associatedValue: newValue, key: AssociatedKeys.customDateFormatter.rawValue, object: self as AnyObject) } } - #endif /// Extract the date components. public var dateComponents: DateComponents { - return region.calendar.dateComponents(DateComponents.allComponentsSet, from: self) + region.calendar.dateComponents(DateComponents.allComponentsSet, from: self) } /// Initialize a new date object from string expressed in given region. @@ -134,7 +120,7 @@ extension Date: DateRepresentable { /// /// - Returns: `DateInRegion` public func inDefaultRegion() -> DateInRegion { - return DateInRegion(self, region: SwiftDate.defaultRegion) + DateInRegion(self, region: SwiftDate.defaultRegion) } /// Express given absolute date in the context of passed region. @@ -142,21 +128,21 @@ extension Date: DateRepresentable { /// - Parameter region: destination region. /// - Returns: `DateInRegion` public func `in`(region: Region) -> DateInRegion { - return DateInRegion(self, region: region) + DateInRegion(self, region: region) } /// Return a date in the distant past. /// /// - Returns: Date instance. public static func past() -> Date { - return Date.distantPast + Date.distantPast } /// Return a date in the distant future. /// /// - Returns: Date instance. public static func future() -> Date { - return Date.distantFuture + Date.distantFuture } } diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift index cc1162cb..43fad5fd 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift @@ -15,7 +15,7 @@ import Foundation // MARK: - Comparing DateInRegion public func == (lhs: DateInRegion, rhs: DateInRegion) -> Bool { - return (lhs.date.timeIntervalSince1970 == rhs.date.timeIntervalSince1970) + (lhs.date.timeIntervalSince1970 == rhs.date.timeIntervalSince1970) } public func <= (lhs: DateInRegion, rhs: DateInRegion) -> Bool { @@ -29,11 +29,11 @@ public func >= (lhs: DateInRegion, rhs: DateInRegion) -> Bool { } public func < (lhs: DateInRegion, rhs: DateInRegion) -> Bool { - return lhs.date.compare(rhs.date) == .orderedAscending + lhs.date.compare(rhs.date) == .orderedAscending } public func > (lhs: DateInRegion, rhs: DateInRegion) -> Bool { - return lhs.date.compare(rhs.date) == .orderedDescending + lhs.date.compare(rhs.date) == .orderedDescending } // The type of comparison to do against today's date or with the suplied date. @@ -120,7 +120,7 @@ public extension DateInRegion { /// - precision: The precision of the comparison (default is 5 minutes, or 300 seconds). /// - Returns: A boolean; true if close by, false otherwise. func compareCloseTo(_ refDate: DateInRegion, precision: TimeInterval = 300) -> Bool { - return (abs(date.timeIntervalSince(refDate.date)) <= precision) + (abs(date.timeIntervalSince(refDate.date)) <= precision) } /// Compare the date with the rule specified in the `compareType` parameter. @@ -277,7 +277,7 @@ public extension DateInRegion { /// /// - returns: `true` if the dates are the same down to the given granularity, otherwise `false` func isInside(date: DateInRegion, granularity: Calendar.Component) -> Bool { - return (compare(toDate: date, granularity: granularity) == .orderedSame) + (compare(toDate: date, granularity: granularity) == .orderedSame) } /// Returns a value between 0.0 and 1.0 or nil, that is the position of current date between 2 other dates. @@ -307,7 +307,7 @@ public extension DateInRegion { /// - granularity: smallest unit that must, along with all larger units, be greater /// - Returns: Boolean 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) + isAfterDate(startDate, orEqual: orEqual, granularity: granularity) && isBeforeDate(endDate, orEqual: orEqual, granularity: granularity) } // MARK: - Date Earlier/Later @@ -317,7 +317,7 @@ public extension DateInRegion { /// - Parameter date: The date to compare to self /// - Returns: The date that is earlier func earlierDate(_ date: DateInRegion) -> DateInRegion { - return self.date.timeIntervalSince(date.date) <= 0 ? self : date + self.date.timeIntervalSince(date.date) <= 0 ? self : date } /// Return the later of two dates, between self and a given date. @@ -325,20 +325,20 @@ public extension DateInRegion { /// - Parameter date: The date to compare to self /// - Returns: The date that is later func laterDate(_ date: DateInRegion) -> DateInRegion { - return self.date.timeIntervalSince(date.date) >= 0 ? self : date + self.date.timeIntervalSince(date.date) >= 0 ? self : date } /// Returns the difference in the calendar component given (like day, month or year) /// with respect to the other date as a positive integer func difference(in component: Calendar.Component, from other: DateInRegion) -> Int? { - return self.date.difference(in: component, from: other.date) + self.date.difference(in: component, from: other.date) } /// Returns the differences in the calendar components given (like day, month and year) /// with respect to the other date as dictionary with the calendar component as the key /// and the diffrence as a positive integer as the value func differences(in components: Set, from other: DateInRegion) -> [Calendar.Component: Int] { - return self.date.differences(in: components, from: other.date) + self.date.differences(in: components, from: other.date) } } diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift index 55e7ffc9..9f312f60 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Components.swift @@ -69,7 +69,7 @@ public extension DateInRegion { /// in 1957 to record the orbit of Sputnik via an IBM 704 (36-bit machine) /// and using only 18 bits until August 7, 2576. var modifiedJulianDay: Double { - return julianDay - 2_400_000.5 + julianDay - 2_400_000.5 } /// Return elapsed time expressed in given components since the current receiver and a reference date. @@ -136,7 +136,7 @@ public extension DateInRegion { /// - Parameter date: The date with which to compare the receiver. /// - Returns: time interval between two dates func timeIntervalSince(_ date: DateInRegion) -> TimeInterval { - return self.date.timeIntervalSince(date.date) + self.date.timeIntervalSince(date.date) } /// Extract DateComponents from the difference between two dates. @@ -144,7 +144,7 @@ public extension DateInRegion { /// - Parameter rhs: date to compare /// - Returns: components func componentsTo(_ rhs: DateInRegion) -> DateComponents { - return calendar.dateComponents(DateComponents.allComponentsSet, from: rhs.date, to: date) + calendar.dateComponents(DateComponents.allComponentsSet, from: rhs.date, to: date) } /// Returns the difference between two dates (`date - self`) expressed as date components. diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift index 5ef2d453..1c9ac74d 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Create.swift @@ -87,7 +87,7 @@ public extension DateInRegion { /// - Parameter list: list to sort /// - Returns: sorted array static func sortedByOldest(list: [DateInRegion]) -> [DateInRegion] { - return list.sorted(by: { $0.date.compare($1.date) == .orderedAscending }) + list.sorted(by: { $0.date.compare($1.date) == .orderedAscending }) } /// Sort date by newest, with the newest date on top. @@ -95,7 +95,7 @@ public extension DateInRegion { /// - Parameter list: list to sort /// - Returns: sorted array static func sortedByNewest(list: [DateInRegion]) -> [DateInRegion] { - return list.sorted(by: { $0.date.compare($1.date) == .orderedDescending }) + list.sorted(by: { $0.date.compare($1.date) == .orderedDescending }) } /// Return the newest date in given list (timezone is ignored, comparison uses absolute date). @@ -120,7 +120,7 @@ public extension DateInRegion { /// - increment: components to add /// - Returns: array of dates static func enumerateDates(from startDate: DateInRegion, to endDate: DateInRegion, increment: DateComponents) -> [DateInRegion] { - return DateInRegion.enumerateDates(from: startDate, to: endDate, increment: { _ in + DateInRegion.enumerateDates(from: startDate, to: endDate, increment: { _ in return increment }) } @@ -154,12 +154,6 @@ public extension DateInRegion { /// - Parameter unit: time unit value. /// - Returns: instance at the beginning of the time unit; `self` if fails. func dateAtStartOf(_ unit: Calendar.Component) -> DateInRegion { - #if os(Linux) - guard let result = (region.calendar as NSCalendar).range(of: unit.nsCalendarUnit, for: date) else { - return self - } - return DateInRegion(result.start, region: region) - #else var start: NSDate? var interval: TimeInterval = 0 guard (region.calendar as NSCalendar).range(of: unit.nsCalendarUnit, start: &start, interval: &interval, for: date), @@ -167,7 +161,6 @@ public extension DateInRegion { return self } return DateInRegion(startDate as Date, region: region) - #endif } /// Return a new DateInRegion that is initialized at the start of the specified components @@ -176,7 +169,7 @@ public extension DateInRegion { /// - Parameter units: sequence of transformations as time unit components /// - Returns: new date at the beginning of the passed components, intermediate results if fails. func dateAtStartOf(_ units: [Calendar.Component]) -> DateInRegion { - return units.reduce(self) { (currentDate, currentUnit) -> DateInRegion in + units.reduce(self) { (currentDate, currentUnit) -> DateInRegion in return currentDate.dateAtStartOf(currentUnit) } } @@ -188,14 +181,6 @@ public extension DateInRegion { /// - returns: A new Moment instance. func dateAtEndOf(_ unit: Calendar.Component) -> DateInRegion { // RangeOfUnit returns the start of the next unit; we will subtract one thousandth of a second - #if os(Linux) - guard let result = (region.calendar as NSCalendar).range(of: unit.nsCalendarUnit, for: date) else { - return self - } - let startOfNextUnit = result.start.addingTimeInterval(result.duration) - let endOfThisUnit = Date(timeInterval: -0.001, since: startOfNextUnit) - return DateInRegion(endOfThisUnit, region: region) - #else var start: NSDate? var interval: TimeInterval = 0 guard (self.region.calendar as NSCalendar).range(of: unit.nsCalendarUnit, start: &start, interval: &interval, for: date), @@ -205,7 +190,6 @@ public extension DateInRegion { let startOfNextUnit = startDate.addingTimeInterval(interval) let endOfThisUnit = Date(timeInterval: -0.001, since: startOfNextUnit as Date) return DateInRegion(endOfThisUnit, region: region) - #endif } /// Return a new DateInRegion that is initialized at the end of the specified components @@ -214,7 +198,7 @@ public extension DateInRegion { /// - Parameter units: sequence of transformations as time unit components /// - Returns: new date at the end of the passed components, intermediate results if fails. func dateAtEndOf(_ units: [Calendar.Component]) -> DateInRegion { - return units.reduce(self) { (currentDate, currentUnit) -> DateInRegion in + units.reduce(self) { (currentDate, currentUnit) -> DateInRegion in return currentDate.dateAtEndOf(currentUnit) } } @@ -460,7 +444,7 @@ public extension DateInRegion { /// - Parameter interval: time interval to shift; maybe negative. /// - Returns: new instance of the `DateInRegion` func addingTimeInterval(_ interval: TimeInterval) -> DateInRegion { - return DateInRegion(date.addingTimeInterval(interval), region: region) + DateInRegion(date.addingTimeInterval(interval), region: region) } // MARK: - Conversion diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion+Math.swift b/Sources/SwiftDate/DateInRegion/DateInRegion+Math.swift index a9e6ff59..d56d55a3 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion+Math.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion+Math.swift @@ -15,7 +15,7 @@ import Foundation // MARK: - Math Operation DateInRegion - DateInRegion public func - (lhs: DateInRegion, rhs: DateInRegion) -> TimeInterval { - return lhs.timeIntervalSince(rhs) + lhs.timeIntervalSince(rhs) } // MARK: - Math Operation DateInRegion - Date Components @@ -26,7 +26,7 @@ public func + (lhs: DateInRegion, rhs: DateComponents) -> DateInRegion { } public func - (lhs: DateInRegion, rhs: DateComponents) -> DateInRegion { - return lhs + (-rhs) + lhs + (-rhs) } // MARK: - Math Operation DateInRegion - Calendar.Component diff --git a/Sources/SwiftDate/DateInRegion/DateInRegion.swift b/Sources/SwiftDate/DateInRegion/DateInRegion.swift index 98133f70..7a913769 100644 --- a/Sources/SwiftDate/DateInRegion/DateInRegion.swift +++ b/Sources/SwiftDate/DateInRegion/DateInRegion.swift @@ -165,14 +165,14 @@ public struct DateInRegion: DateRepresentable, Decodable, Encodable, CustomStrin /// /// - Returns: Date instance. public static func past() -> DateInRegion { - return DateInRegion(Date.distantPast, region: SwiftDate.defaultRegion) + DateInRegion(Date.distantPast, region: SwiftDate.defaultRegion) } /// Return a date in the distant future. /// /// - Returns: Date instance. public static func future() -> DateInRegion { - return DateInRegion(Date.distantFuture, region: SwiftDate.defaultRegion) + DateInRegion(Date.distantFuture, region: SwiftDate.defaultRegion) } // MARK: - Codable Support diff --git a/Sources/SwiftDate/DateInRegion/Region.swift b/Sources/SwiftDate/DateInRegion/Region.swift index 8818a95e..23707f86 100644 --- a/Sources/SwiftDate/DateInRegion/Region.swift +++ b/Sources/SwiftDate/DateInRegion/Region.swift @@ -29,7 +29,7 @@ public struct Region: Decodable, Encodable, Equatable, Hashable, CustomStringCon /// Description of the object public var description: String { - return "{calendar='\(calendar.identifier)', timezone='\(timeZone.identifier)', locale='\(locale.identifier)'}" + "{calendar='\(calendar.identifier)', timezone='\(timeZone.identifier)', locale='\(locale.identifier)'}" } public func hash(into hasher: inout Hasher) { diff --git a/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift b/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift index 6a5725e5..e9e28ca4 100644 --- a/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift +++ b/Sources/SwiftDate/Foundation+Extras/Int+DateComponents.swift @@ -34,47 +34,47 @@ public extension Int { /// Create a `DateComponents` with `self` value set as nanoseconds var nanoseconds: DateComponents { - return toDateComponents(type: .nanosecond) + toDateComponents(type: .nanosecond) } /// Create a `DateComponents` with `self` value set as seconds var seconds: DateComponents { - return toDateComponents(type: .second) + toDateComponents(type: .second) } /// Create a `DateComponents` with `self` value set as minutes var minutes: DateComponents { - return toDateComponents(type: .minute) + toDateComponents(type: .minute) } /// Create a `DateComponents` with `self` value set as hours var hours: DateComponents { - return toDateComponents(type: .hour) + toDateComponents(type: .hour) } /// Create a `DateComponents` with `self` value set as days var days: DateComponents { - return toDateComponents(type: .day) + toDateComponents(type: .day) } /// Create a `DateComponents` with `self` value set as weeks var weeks: DateComponents { - return toDateComponents(type: .weekOfYear) + toDateComponents(type: .weekOfYear) } /// Create a `DateComponents` with `self` value set as months var months: DateComponents { - return toDateComponents(type: .month) + toDateComponents(type: .month) } /// Create a `DateComponents` with `self` value set as years var years: DateComponents { - return toDateComponents(type: .year) + toDateComponents(type: .year) } /// Create a `DateComponents` with `self` value set as quarters var quarters: DateComponents { - return toDateComponents(type: .quarter) + toDateComponents(type: .quarter) } } diff --git a/Sources/SwiftDate/Foundation+Extras/String+Parser.swift b/Sources/SwiftDate/Foundation+Extras/String+Parser.swift index e9dc6ecf..bb2273d0 100644 --- a/Sources/SwiftDate/Foundation+Extras/String+Parser.swift +++ b/Sources/SwiftDate/Foundation+Extras/String+Parser.swift @@ -87,23 +87,23 @@ public protocol DateParsable { extension String: DateParsable { public func toDate(_ format: String? = nil, region: Region = SwiftDate.defaultRegion) -> DateInRegion? { - return DateInRegion(self, format: format, region: region) + DateInRegion(self, format: format, region: region) } public func toDate(_ formats: [String], region: Region) -> DateInRegion? { - return DateInRegion(self, formats: formats, region: region) + DateInRegion(self, formats: formats, region: region) } public func toDate(style: StringToDateStyles, region: Region = SwiftDate.defaultRegion) -> DateInRegion? { - return style.toDate(self, region: region) + style.toDate(self, region: region) } public func toISODate(_ options: ISOParser.Options? = nil, region: Region? = nil) -> DateInRegion? { - return ISOParser.parse(self, region: region, options: options) + ISOParser.parse(self, region: region, options: options) } public func toDotNETDate(region: Region = Region.ISO) -> DateInRegion? { - return DOTNETParser.parse(self, region: region, options: nil) + DOTNETParser.parse(self, region: region, options: nil) } public func toRSSDate(alt: Bool, region: Region = Region.ISO) -> DateInRegion? { @@ -114,10 +114,11 @@ extension String: DateParsable { } public func toSQLDate(region: Region = Region.ISO) -> DateInRegion? { - return StringToDateStyles.sql.toDate(self, region: region) + StringToDateStyles.sql.toDate(self, region: region) } public func asLocale() -> Locale { - Locale(identifier: self) - } + Locale(identifier: self) + } + } diff --git a/Sources/SwiftDate/Foundation+Extras/TimeInterval+Formatter.swift b/Sources/SwiftDate/Foundation+Extras/TimeInterval+Formatter.swift index 66bd0073..1bee7f7c 100644 --- a/Sources/SwiftDate/Foundation+Extras/TimeInterval+Formatter.swift +++ b/Sources/SwiftDate/Foundation+Extras/TimeInterval+Formatter.swift @@ -162,7 +162,7 @@ public extension TimeInterval { /// /// - returns: the value of interval expressed in selected `Calendar.Component` func toUnit(_ component: Calendar.Component, to refDate: DateInRegion? = nil) -> Int? { - return toUnits([component], to: refDate)[component] + toUnits([component], to: refDate)[component] } } diff --git a/Sources/SwiftDate/Supports/AssociatedValues.swift b/Sources/SwiftDate/Supports/AssociatedValues.swift index 4b671d85..1ec27073 100644 --- a/Sources/SwiftDate/Supports/AssociatedValues.swift +++ b/Sources/SwiftDate/Supports/AssociatedValues.swift @@ -10,22 +10,19 @@ // Copyright © 2019 Daniele Margutti. Licensed under MIT License. // -#if os(Linux) - -#else import Foundation import ObjectiveC.runtime internal func getAssociatedValue(key: String, object: AnyObject) -> T? { - return (objc_getAssociatedObject(object, key.address) as? AssociatedValue)?.value as? T + (objc_getAssociatedObject(object, key.address) as? AssociatedValue)?.value as? T } internal func getAssociatedValue(key: String, object: AnyObject, initialValue: @autoclosure () -> T) -> T { - return getAssociatedValue(key: key, object: object) ?? setAndReturn(initialValue: initialValue(), key: key, object: object) + getAssociatedValue(key: key, object: object) ?? setAndReturn(initialValue: initialValue(), key: key, object: object) } internal func getAssociatedValue(key: String, object: AnyObject, initialValue: () -> T) -> T { - return getAssociatedValue(key: key, object: object) ?? setAndReturn(initialValue: initialValue(), key: key, object: object) + getAssociatedValue(key: key, object: object) ?? setAndReturn(initialValue: initialValue(), key: key, object: object) } private func setAndReturn(initialValue: T, key: String, object: AnyObject) -> T { @@ -71,4 +68,3 @@ private class AssociatedValue { } } -#endif diff --git a/Tests/SwiftDateTests/TestFormatters.swift b/Tests/SwiftDateTests/TestFormatters.swift index b6141bdc..44fc58d8 100644 --- a/Tests/SwiftDateTests/TestFormatters.swift +++ b/Tests/SwiftDateTests/TestFormatters.swift @@ -456,14 +456,8 @@ class TestFormatters: XCTestCase { func testTimeInterval_Clock() { let value = (2.hours + 5.minutes).timeInterval.toClock() XCTAssert(value == "02:05:00", "Failed to format clock") - #if os(Linux) - let zeroBehavior = DateComponentsFormatter.ZeroFormattingBehavior(rawValue: 14) - let value2 = (4.minutes + 50.minutes).timeInterval.toClock(zero: zeroBehavior) - XCTAssert(value2 == "54:00", "Failed to format clock") - #else let value2 = (4.minutes + 50.minutes).timeInterval.toClock(zero: DateComponentsFormatter.ZeroFormattingBehavior.dropAll) XCTAssert(value2 == "54", "Failed to format clock") - #endif } func testFormatterCustom() {