Skip to content
This repository has been archived by the owner on Mar 29, 2018. It is now read-only.

Commit

Permalink
Add Comparable functions to NSDate
Browse files Browse the repository at this point in the history
  • Loading branch information
PGLongo committed Jan 10, 2015
1 parent 8cf9be7 commit f38c696
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
27 changes: 26 additions & 1 deletion ExSwift/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,29 @@ public extension NSDate{
let components = calendar.components(component, fromDate: self)
return components.valueForComponent(component)
}
}
}

// MARK: Comparable functions

public func ==(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == NSComparisonResult.OrderedSame
}

public func <(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs.compare(rhs) == NSComparisonResult.OrderedAscending
}

public func >(lhs: NSDate, rhs: NSDate) -> Bool {
return !(lhs < rhs)
}

public func <=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs < rhs || lhs == rhs
}

public func >=(lhs: NSDate, rhs: NSDate) -> Bool {
return lhs > rhs || lhs == rhs
}

extension NSDate: Comparable {
}
54 changes: 53 additions & 1 deletion ExSwiftTests/ExSwiftNSDateTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ class ExSwiftNSDataTests: XCTestCase {
XCTAssertFalse(date.isAfter(date), "Past date should be in the past")
XCTAssertFalse(pastDate.isAfter(date), "Past date should be in the past")
}

func testIsBefore(){
var date = NSDate()
var futureDate = date.addSeconds(42)
Expand All @@ -159,6 +159,8 @@ class ExSwiftNSDataTests: XCTestCase {

}

// MARK: Getter

func testGetter() {
XCTAssertEqual(1988, startDate!.year, "Year Mismatch")
XCTAssertEqual(11, startDate!.month, "Month Mismatch")
Expand All @@ -170,4 +172,54 @@ class ExSwiftNSDataTests: XCTestCase {
XCTAssertEqual(5, startDate!.weekMonth, "Weekmonth Mismatch")
}

// MARK: Comparable

func testSorting () {
var firstDate = startDate!.addSeconds(0)
var secondDate = startDate!.addSeconds(42)
var thirdDate = startDate!.addSeconds(-42)
var fourthDate = startDate!.addSeconds(-84)
var fifthDate = startDate!.addSeconds(84)

var dates : [NSDate] = [thirdDate, secondDate, firstDate, fourthDate, fifthDate]

let expected : [NSDate] = [fifthDate, secondDate, firstDate, thirdDate, fourthDate]

let expectedReverded = expected.reverse()

for i in 0 ... 42 {
dates.shuffle()

dates.sort( { $0 > $1 } )
XCTAssertEqual(expected, dates, "Sort mismatch")

dates.sort( { $0 < $1 } )
XCTAssertEqual(expectedReverded, dates, "Sort mismatch")
}
}

func testComparable(){
var date = startDate!.addSeconds(-42)
var anotherDate = startDate!.addSeconds(42)
let shouldBeTheSameDate = NSDate(timeInterval: 0, sinceDate: startDate!)


XCTAssertTrue(startDate > date, "Date should be greater")
XCTAssertFalse(startDate > anotherDate, "Date shouldn't be greater")

XCTAssertTrue(startDate < anotherDate, "Date should be lower")
XCTAssertFalse(startDate < date, "Date shouldn't be lower")

XCTAssertTrue(startDate >= shouldBeTheSameDate, "Date should be greater or equal")
XCTAssertTrue(startDate >= date, "Date should be greater or equal")
XCTAssertFalse(startDate >= anotherDate, "Date shouldn't be greater or equal")

XCTAssertTrue(startDate <= shouldBeTheSameDate, "Date should be lower or equal")
XCTAssertTrue(startDate < anotherDate, "Date should be lower")
XCTAssertFalse(startDate <= date, "Date shouldn't be greater or equal")

XCTAssertFalse(date == startDate, "Date should mismatch")
XCTAssertFalse(anotherDate == startDate, "Date should mismatch")
XCTAssertTrue(shouldBeTheSameDate == startDate, "Date shouldn't mismatch")
}
}

0 comments on commit f38c696

Please sign in to comment.