Skip to content

Instantly share code, notes, and snippets.

@mokagio
Created May 20, 2016 05:36
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 mokagio/002122061fd76729c86702174498f458 to your computer and use it in GitHub Desktop.
Save mokagio/002122061fd76729c86702174498f458 to your computer and use it in GitHub Desktop.
This NSDate extension provides a builder method intended to be used to simplify creating dates in unit tests.
import Foundation
/// This `NSDate` extension provides a builder method intended to be used to
/// simplify creating dates in unit tests.
extension NSDate {
static func date(
year year: Int = 1970,
month: Int = 01,
day: Int = 01,
hour: Int = 00,
minute: Int = 00,
second: Int = 00,
millisecond: Int = 000
) -> NSDate {
let components = NSDateComponents()
components.calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
components.timeZone = NSTimeZone(forSecondsFromGMT: 0)
components.year = year
components.month = month
components.day = day
components.hour = hour
components.minute = minute
components.second = second
components.nanosecond = millisecond * 1000000
guard let date = components.date else {
preconditionFailure("Could not init NSDate from NSDateComponents")
}
return date
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment