Skip to content

Instantly share code, notes, and snippets.

@ohtwo
Forked from algal/NSDate+RFC3339.swift
Created November 19, 2015 06:49
Show Gist options
  • Save ohtwo/15f872475c8349e101d4 to your computer and use it in GitHub Desktop.
Save ohtwo/15f872475c8349e101d4 to your computer and use it in GitHub Desktop.
NSDate+RFC3339
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