Last active
August 29, 2015 14:24
-
-
Save SteveTrewick/7a60f97746d74aa1e8b2 to your computer and use it in GitHub Desktop.
Parse an RFC 822 date (e.g. for RSS/Atom feeds) based on some easy to spot characteristics of the format.
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
// Because [EEE, ]dd MMM yyyy HH:mm[:ss] z | |
// we can split based on , and : and count. | |
// There is probably a better way to do this | |
// without splitting the strings. | |
func dateFromPubdateString(str:String) -> NSDate? { | |
func hasDay(str:String) -> Bool { | |
return str.componentsSeparatedByString(",").count > 1 | |
} | |
func hasSeconds(str: String) -> Bool { | |
return str.componentsSeparatedByString(":").count > 2 | |
} | |
var format = "" | |
let formatter = NSDateFormatter() | |
format += hasDay(str) ? "EEE, " : "" | |
format += "dd MMM yyyy HH:mm" | |
format += hasSeconds(str) ? ":ss z" : " z" | |
formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX") | |
formatter.dateFormat = format | |
return formatter.dateFromString(str) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment