In my current project I found that I was coming back to the same section of code repeatedly and that I needed to refactor it out as well to maintain it, as I new that I would be back here quite a bit. The particular section of code was used for parsing date time strings into NSDate objects, the strings coming from RSS and Atom XML.
I had come across a few articles on parsing internet dates doing just this already and had seen a bit of code on GitHub doing this task as well. But what I didn’t find is a project doing just this on GitHub.
So I made one.
You can see it here. https://github.com/OrionSeven/BSNSDate-InternetDateParsing
This isn’t my first project on GitHub, but it is my first Objective-C one, and once more it’s something I actually need and hope others will too. To use it simply add the files in your project, include the header file and:
|
1 2 |
NSString *dateTimeString = @"2013-02-01T10:52:00Z"; NSDate *date = [NSDate dateFromInternetDateTimeString:dateTimeString]; |
Or if you think you know the format used for your date you can give it a hint:
|
1 2 |
NSString *dateTimeString = @"Mon Feb 11 2013 12:53:19 GMT-0800 (PST)"; NSDate *date = [NSDate dateFromInternetDateTimeString:test5 FormatHint:RFC2822]; |
The NSDate Category supports parsing RFC 2822 and 3339 formatted dates. Right now it’s just the few common ones I’ve come across, but it handles the heavy lifting in a nice manner for you.
This is in large part based on Apples documentation about NSDateFormatter and Internet Dates.