Skip to content

Make NSDate conform to Comparable protocol #31

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

Closed
wants to merge 5 commits into from
Closed
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: 4 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
5BF7AEC01BCD51F9008F214A /* NSUUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4B1BCC5DCB00ED97BB /* NSUUID.swift */; };
5BF7AEC11BCD51F9008F214A /* NSValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDC3F4C1BCC5DCB00ED97BB /* NSValue.swift */; };
E876A73E1C1180E000F279EC /* TestNSRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = E876A73D1C1180E000F279EC /* TestNSRange.swift */; };
9C7684F01C11D9B50046490B /* TestNSDate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9C7684EF1C11D9B50046490B /* TestNSDate.swift */; settings = {ASSET_TAGS = (); }; };
EA66F6361BEED03E00136161 /* TargetConditionals.h in Headers */ = {isa = PBXBuildFile; fileRef = EA66F6351BEED03E00136161 /* TargetConditionals.h */; settings = {ATTRIBUTES = (Public, ); }; };
EA66F6441BF1619600136161 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA66F6381BF1619600136161 /* main.swift */; };
EA66F6481BF1619600136161 /* NSURLTestData.plist in Resources */ = {isa = PBXBuildFile; fileRef = EA66F63B1BF1619600136161 /* NSURLTestData.plist */; };
Expand Down Expand Up @@ -513,6 +514,7 @@
5BDC405C1BD6D83B00ED97BB /* TestFoundation.app */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TestFoundation.app; sourceTree = BUILT_PRODUCTS_DIR; };
5BF7AEC21BCD568D008F214A /* ForSwiftFoundationOnly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ForSwiftFoundationOnly.h; sourceTree = "<group>"; };
E876A73D1C1180E000F279EC /* TestNSRange.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSRange.swift; sourceTree = "<group>"; };
9C7684EF1C11D9B50046490B /* TestNSDate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSDate.swift; sourceTree = "<group>"; };
EA313DFC1BE7F2E90060A403 /* CFURLComponents_Internal.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CFURLComponents_Internal.h; sourceTree = "<group>"; };
EA313DFD1BE7F2E90060A403 /* CFURLComponents_URIParser.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFURLComponents_URIParser.c; sourceTree = "<group>"; };
EA313DFE1BE7F2E90060A403 /* CFURLComponents.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = CFURLComponents.c; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1012,6 +1014,7 @@
5BC1D8BC1BF3ADFE009D3973 /* TestNSCharacterSet.swift */,
525AECEB1BF2C96400D15BB0 /* TestNSFileManager.swift */,
5B40F9F11C125187000E72E3 /* TestNSXMLParser.swift */,
9C7684EF1C11D9B50046490B /* TestNSDate.swift */,
);
name = Tests;
sourceTree = "<group>";
Expand Down Expand Up @@ -1701,6 +1704,7 @@
EA66F64E1BF1619600136161 /* TestNSIndexSet.swift in Sources */,
EA66F6541BF1619600136161 /* TestNSSet.swift in Sources */,
EA66F64A1BF1619600136161 /* TestNSArray.swift in Sources */,
9C7684F01C11D9B50046490B /* TestNSDate.swift in Sources */,
5BC1D8BE1BF3B09E009D3973 /* TestNSCharacterSet.swift in Sources */,
EA66F6561BF1619600136161 /* TestNSString.swift in Sources */,
5B40F9F21C125187000E72E3 /* TestNSXMLParser.swift in Sources */,
Expand Down
10 changes: 10 additions & 0 deletions Foundation/NSDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ extension CFDateRef : _NSBridgable {
internal var _nsObject: NSType { return unsafeBitCast(self, NSType.self) }
}

extension NSDate : Comparable { }

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

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

/// Alternative API for avoiding AutoreleasingUnsafeMutablePointer usage in NSCalendar and NSFormatter
/// - Experiment: This is a draft API currently under consideration for official import into Foundation as a suitable alternative to the AutoreleasingUnsafeMutablePointer usage case of returning a NSDate + NSTimeInterval or using a pair of dates representing a range
/// - Note: Since this API is under consideration it may be either removed or revised in the near future
Expand Down
46 changes: 46 additions & 0 deletions TestFoundation/TestNSDate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

#if DEPLOYMENT_RUNTIME_OBJC || os(Linux)
import Foundation
import XCTest
#else
import SwiftFoundation
import SwiftXCTest
#endif

class TestNSDate : XCTestCase {

var allTests : [(String, () -> ())] {
return [
("test_comparable", test_comparable ),
]
}

func ignoreError(@noescape block: () throws -> Void) {
do { try block() } catch { }
}

func test_comparable() {

let past = NSDate.distantPast()
let future = NSDate.distantFuture()
let present = NSDate()

XCTAssertTrue(past < present)
XCTAssertTrue(past <= present)

XCTAssertTrue(past != present)
XCTAssertTrue(present == present)

XCTAssertTrue(future > present)
XCTAssertTrue(future >= present)
}

}
1 change: 1 addition & 0 deletions TestFoundation/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ XCTMain([
TestNSFileManger(),
TestNSRange(),
TestNSXMLParser(),
TestNSDate(),
])