Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

earlierDate and laterDate optimisation #689

Merged
merged 4 commits into from
Sep 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/SwiftDate/Date/Date+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ public extension Date {
/// - Parameter date: The date to compare to self
/// - Returns: The date that is earlier
func earlierDate(_ date: Date) -> Date {
return (timeIntervalSince1970 <= date.timeIntervalSince1970) ? self : date
return timeIntervalSince(date) <= 0 ? 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
func laterDate(_ date: Date) -> Date {
return (timeIntervalSince1970 >= date.timeIntervalSince1970) ? self : date
return timeIntervalSince(date) >= 0 ? self : date
}

}
4 changes: 2 additions & 2 deletions Sources/SwiftDate/DateInRegion/DateInRegion+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -299,15 +299,15 @@ 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.timeIntervalSince1970 <= date.date.timeIntervalSince1970) ? self : date
return self.date.timeIntervalSince(date.date) <= 0 ? 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
func laterDate(_ date: DateInRegion) -> DateInRegion {
return (self.date.timeIntervalSince1970 >= date.date.timeIntervalSince1970) ? self : date
return self.date.timeIntervalSince(date.date) >= 0 ? self : date
}

}
6 changes: 5 additions & 1 deletion Tests/SwiftDateTests/TestDateInRegion+Compare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,14 @@ class TestDateInRegion_Compare: XCTestCase {
// earlierDate()
XCTAssert( (date1.earlierDate(date2) == date1), "Failed to get .earlierDate()")
XCTAssert( (date1.earlierDate(date3) == date1), "Failed to get .earlierDate()")

XCTAssert( (date1.date.earlierDate(date2.date) == date1.date), "Failed to get .earlierDate()")
XCTAssert( (date1.date.earlierDate(date3.date) == date1.date), "Failed to get .earlierDate()")

// laterDate()
XCTAssert( (date1.laterDate(date2) == date2), "Failed to get .laterDate()")
XCTAssert( (date1.laterDate(date3) == date3), "Failed to get .laterDate()")
XCTAssert( (date1.date.laterDate(date2.date) == date2.date), "Failed to get .laterDate()")
XCTAssert( (date1.date.laterDate(date3.date) == date3.date), "Failed to get .laterDate()")
}

func testDateInRegion_compareMath() {
Expand Down