Skip to content

Commit

Permalink
[Swift] Make date and time structs comparable
Browse files Browse the repository at this point in the history
  • Loading branch information
p2 committed Mar 24, 2015
1 parent 37e307a commit 3b0175e
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ isodate
[fhir]: http://www.hl7.org/implement/standards/fhir/
[jinja]: http://jinja.pocoo.org
[swift]: https://developer.apple.com/swift/
[swift-fhir]: https://github.com/p2/Swift-FHIR
[swift-fhir]: https://github.com/smart-on-fhir/Swift-FHIR
60 changes: 59 additions & 1 deletion Swift/DateAndTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
/**
A protocol for all our date and time structs.
*/
protocol DateAndTime: Printable
protocol DateAndTime: Printable, Comparable
{
var nsDate: NSDate { get set }
}
Expand Down Expand Up @@ -99,6 +99,24 @@ public struct Date: DateAndTime
}
}

public func <(lhs: Date, rhs: Date) -> Bool {
if lhs.year == rhs.year {
if lhs.month == rhs.month {
return lhs.day < rhs.day
}
return lhs.month < rhs.month
}
return lhs.year < rhs.year
}

public func ==(lhs: Date, rhs: Date) -> Bool {
return lhs.year == rhs.year
&& lhs.month == rhs.month
&& lhs.day == rhs.day
}



/**
A time during the day, optionally with seconds, usually for human communication.

Expand Down Expand Up @@ -212,6 +230,22 @@ public struct Time: DateAndTime
}
}

public func <(lhs: Time, rhs: Time) -> Bool {
if lhs.hour == rhs.hour {
if lhs.minute == rhs.minute {
return lhs.second < rhs.second
}
return lhs.minute < rhs.minute
}
return lhs.hour < rhs.hour
}

public func ==(lhs: Time, rhs: Time) -> Bool {
return lhs.hour == rhs.hour
&& lhs.minute == rhs.minute
&& lhs.second == rhs.second
}


/**
A date, optionally with time, as used in human communication.
Expand Down Expand Up @@ -298,6 +332,18 @@ public struct DateTime: DateAndTime
}
}

public func <(lhs: DateTime, rhs: DateTime) -> Bool {
let lhd = lhs.nsDate
let rhd = rhs.nsDate
return (lhd.compare(rhd) == .OrderedAscending)
}

public func ==(lhs: DateTime, rhs: DateTime) -> Bool {
let lhd = lhs.nsDate
let rhd = rhs.nsDate
return (lhd.compare(rhd) == .OrderedSame)
}


/**
An instant in time, known at least to the second and with a timezone, for machine times.
Expand Down Expand Up @@ -387,6 +433,18 @@ public struct Instant: DateAndTime
}
}

public func <(lhs: Instant, rhs: Instant) -> Bool {
let lhd = lhs.nsDate
let rhd = rhs.nsDate
return (lhd.compare(rhd) == .OrderedAscending)
}

public func ==(lhs: Instant, rhs: Instant) -> Bool {
let lhd = lhs.nsDate
let rhd = rhs.nsDate
return (lhd.compare(rhd) == .OrderedSame)
}


/**
Converts between NSDate and our Date, Time, DateTime and Instance structs.
Expand Down
146 changes: 146 additions & 0 deletions Swift/DateAndTimeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,38 @@ class DateTests: XCTestCase
XCTAssertTrue(nil == d!.month)
XCTAssertTrue(nil == d!.day)
}

func testComparisons() {
var a = Date(string: "2014")!
var b = Date(string: "1914")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Date(string: "2014-12")!
b = Date(string: "2014-11")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Date(string: "2014-11-25")!
b = Date(string: "2014-11-24")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Date(string: "2014-11-24")!
b = Date(string: "1914-11-24")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Date(string: "2014-12-24")!
b = Date(string: "2014-11-24")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)
}
}


Expand Down Expand Up @@ -109,6 +141,32 @@ class TimeTests: XCTestCase
t = Time(string: "abc")
XCTAssertTrue(nil == t)
}

func testComparisons() {
var a = Time(string: "19:12")!
var b = Time(string: "19:11")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Time(string: "19:11:04")!
b = Time(string: "19:11:03")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Time(string: "19:11:04")!
b = Time(string: "07:11:05")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Time(string: "19:11:04.0002")!
b = Time(string: "19:11:04")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)
}
}


Expand Down Expand Up @@ -296,6 +354,62 @@ class DateTimeTests: XCTestCase
XCTAssertFalse(nil == d!.timeZone)
XCTAssertEqual(NSTimeZone.localTimeZone(), d!.timeZone!, "Should default to local time zone but have \(d!.timeZone)")
}

func testComparisons() {
var a = DateTime(string: "2014")!
var b = DateTime(string: "1914")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11")!
b = DateTime(string: "2015-03-28T03:10")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11:04")!
b = DateTime(string: "2015-03-28T03:11:03")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11:04")!
b = DateTime(string: "2015-03-28T03:11:04")!
XCTAssertFalse(a > b)
XCTAssertTrue(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11:04Z")!
b = DateTime(string: "1915-03-28T03:11:04Z")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11:04Z")!
b = DateTime(string: "2015-03-28T03:11:04+00:00")!
XCTAssertFalse(a > b)
XCTAssertTrue(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T03:11+00:00")!
b = DateTime(string: "2015-03-28T03:17+01:00")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-27T22:12:44-05:00")!
b = DateTime(string: "2015-03-28T03:12:43-00:00")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = DateTime(string: "2015-03-28T05:11:44.3+01:00")!
b = DateTime(string: "2015-03-27T23:11:44.3-05:00")!
XCTAssertFalse(a > b)
XCTAssertTrue(a == b)
XCTAssertTrue(a == a)
}
}


Expand Down Expand Up @@ -347,5 +461,37 @@ class InstantTests: XCTestCase
XCTAssertEqual(29.1285, d!.time.second!)
XCTAssertTrue(-18000 == d!.timeZone.secondsFromGMT)
}

func testComparisons() {
var a = Instant(string: "2015-03-28T03:11:04Z")!
var b = Instant(string: "1915-03-28T03:11:04Z")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Instant(string: "2015-03-28T03:11:04Z")!
b = Instant(string: "2015-03-28T03:11:04+00:00")!
XCTAssertFalse(a > b)
XCTAssertTrue(a == b)
XCTAssertTrue(a == a)

a = Instant(string: "2015-03-28T03:11:44+00:00")!
b = Instant(string: "2015-03-28T03:17:44+01:00")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Instant(string: "2015-03-27T22:12:44-05:00")!
b = Instant(string: "2015-03-28T03:11:44-00:00")!
XCTAssertTrue(a > b)
XCTAssertFalse(a == b)
XCTAssertTrue(a == a)

a = Instant(string: "2015-03-28T05:11:44.3+01:00")!
b = Instant(string: "2015-03-27T23:11:44.3-05:00")!
XCTAssertFalse(a > b)
XCTAssertTrue(a == b)
XCTAssertTrue(a == a)
}
}

3 changes: 2 additions & 1 deletion Swift/FHIRTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct Base64Binary: Printable, Equatable, Comparable
// MARK: - Printable

public var description: String {
return value ?? ""
return "<Base64Binary; \(nil != value ? countElements(value!) : 0) chars>"
}
}

Expand All @@ -39,3 +39,4 @@ public func <(lhs: Base64Binary, rhs: Base64Binary) -> Bool {
public func ==(lhs: Base64Binary, rhs: Base64Binary) -> Bool {
return lhs.value == rhs.value
}

0 comments on commit 3b0175e

Please sign in to comment.