Skip to content

Instantly share code, notes, and snippets.

@SteveTrewick
Last active August 29, 2015 14:24
Show Gist options
  • Save SteveTrewick/7a60f97746d74aa1e8b2 to your computer and use it in GitHub Desktop.
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.
// 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