-
-
Save ohtwo/15f872475c8349e101d4 to your computer and use it in GitHub Desktop.
NSDate+RFC3339
This file contains 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
extension NSDate { | |
var stringFormattedAsRFC3339: String { | |
return rfc3339formatter.stringFromDate(self) | |
} | |
class func dateFromRFC3339FormattedString(rfc3339FormattedString:String) -> NSDate? | |
{ | |
/* | |
NOTE: will replace this with a failible initializer when Apple fixes the bug | |
that requires the initializer to initialize all stored properties before returning nil, | |
even when this is impossible. | |
*/ | |
if let d = rfc3339formatter.dateFromString(rfc3339FormattedString) | |
{ | |
return NSDate(timeInterval:0,sinceDate:d) | |
} | |
else { | |
return nil | |
} | |
} | |
} | |
private var rfc3339formatter:NSDateFormatter = { | |
let formatter = NSDateFormatter() | |
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" | |
formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0) | |
formatter.calendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierISO8601)! | |
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") | |
return formatter | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment