Created
May 12, 2015 12:52
-
-
Save siemensikkema/86f19f48d4306e4fa85d to your computer and use it in GitHub Desktop.
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 | |
/// converts a string representing an unlocalized date with fixed-format to NSDate using Unix methods (suggested in Apple's Date Formatting guide: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html) | |
public func timeStringToDate(timeString: NSString, formatString: NSString) -> NSDate { | |
var t = tm() | |
return withUnsafeMutablePointer(&t) { t -> NSDate in | |
strptime_l(timeString.UTF8String, formatString.UTF8String, t, nil) | |
return NSDate(timeIntervalSince1970: NSTimeInterval(mktime(t))) | |
} | |
} | |
/// converts a string representing a date in the common ISO8601 format to NSDate | |
public func iso8601DateStringToDate(var iso8601DateString: String) -> NSDate? { | |
if count(iso8601DateString) == 25 && | |
// remove extra ':' in timezone specification | |
iso8601DateString.removeAtIndex(advance(iso8601DateString.endIndex, -3)) == ":" { | |
return timeStringToDate(iso8601DateString, "%Y-%m-%dT%H:%M:%S%z") | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment