Created
May 20, 2016 05:36
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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