Skip to content

[5.3] Date: Add distance(to:) and advanced(by:) introduced in macOS 10.15 #2890

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

Merged
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
17 changes: 16 additions & 1 deletion Sources/Foundation/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,22 @@ public struct Date : ReferenceConvertible, Comparable, Equatable {
public static func -=(lhs: inout Date, rhs: TimeInterval) {
lhs = lhs - rhs
}


public typealias Stride = TimeInterval

/// Returns the `TimeInterval` between this `Date` and another given date.
///
/// - returns: The interval between the receiver and the another parameter. If the receiver is earlier than `other`, the return value is negative.
public func distance(to other: Date) -> TimeInterval {
return other.timeIntervalSince(self)
}

/// Creates a new date value by adding a `TimeInterval` to this `Date`.
///
/// - warning: This only adjusts an absolute value. If you wish to add calendrical concepts like hours, days, months then you must use a `Calendar`. That will take into account complexities like daylight saving time, months with different numbers of days, and more.
public func advanced(by n: TimeInterval) -> Date {
return self.addingTimeInterval(n)
}
}

extension Date : CustomDebugStringConvertible, CustomStringConvertible, CustomReflectable {
Expand Down
20 changes: 20 additions & 0 deletions Tests/Foundation/Tests/TestDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class TestDate : XCTestCase {
("test_timeIntervalSinceReferenceDate", test_timeIntervalSinceReferenceDate),
("test_recreateDateComponentsFromDate", test_recreateDateComponentsFromDate),
("test_Hashing", test_Hashing),
("test_advancedBy", test_advancedBy),
("test_distanceTo", test_distanceTo),
]
}

Expand Down Expand Up @@ -172,4 +174,22 @@ class TestDate : XCTestCase {
]
checkHashable(values, equalityOracle: { $0 == $1 })
}

func test_advancedBy() {
let date1 = dateWithString("2010-05-17 14:49:47 -0000")
let date2 = dateWithString("2010-05-18 14:49:47 -0000")

XCTAssertEqual(date1.advanced(by: 86400), date2)
XCTAssertEqual(date2.advanced(by: -86400), date1)
XCTAssertEqual(date1.advanced(by: 0), date1)
}

func test_distanceTo() {
let date1 = dateWithString("2010-05-17 14:49:47 -0000")
let date2 = dateWithString("2010-05-18 14:49:47 -0000")

XCTAssertEqual(date1.distance(to: date2), 86400)
XCTAssertEqual(date2.distance(to: date1), -86400)
XCTAssertEqual(date1.distance(to: date1), 0)
}
}