Skip to content

Instantly share code, notes, and snippets.

@AsceticMonk
Last active June 23, 2016 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AsceticMonk/ecedd0ba7780468bc72de71a8b4fa26d to your computer and use it in GitHub Desktop.
Save AsceticMonk/ecedd0ba7780468bc72de71a8b4fa26d to your computer and use it in GitHub Desktop.
New value type in Foundation
import Foundation
// DateInterval, new value type
let interval1 = DateInterval()
interval1.duration
print("Same start and end date? \(interval1.start == interval1.end)") // True
// Creating a date interval between now and 1 month later
let interval2 = DateInterval(start: Date(), duration: TimeInterval(2_628_000))
interval2.duration
print("Same interval? \(interval1 == interval2)") // False
// Containment
let oneWeekLater = Date(timeIntervalSinceNow: TimeInterval(604_800))
let oneYearLater = Date(timeIntervalSinceNow: TimeInterval(3_1540_000))
interval2.contains(oneWeekLater) // True
interval2.contains(oneYearLater) // False
// Intersection
let interval3 = DateInterval(start: Date(), end: oneWeekLater)
interval2.intersects(interval3) // True
let intersection = interval2.intersection(with: interval3)
if let intersection = intersection {
print("The intersection starts from \(intersection.start) to \(intersection.end)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment