-
Parsing RFC2822 dates with NSDate
Sept. 12, 2008 at 03:26:12 CESTSince the iPhone doesn't include the handy NSCalendarDate class, parsing RFC2822 dates is not so obvious as it used to be. Now, we need to use a NSDateFormatter with the appropiate format set and call -dateWithString:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; /* This is required, Cocoa will try to use the current locale otherwise */ NSLocale *enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [formatter setLocale:enUS]; [enUS release]; [formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"]; /* Unicode Locale Data Markup Language */ NSDate *theDate = [formatter dateWithString:rfc2822String]; /*e.g. @"Thu, 11 Sep 2008 12:34:12 +0200" */
However, this approach isn't very good because we need to initialize and configure a new formatter everytime we need to parse a date. Why not write a category?
//NSDate+RFC2822.h #import <Foundation/Foundation.h> @interface NSDate (RFC2822) + (NSDate)dateFromRFC2822:(NSString *)rfc2822; @end //NSDate+RFC2822.m #import "NSDate+RFC2822.h" @implementation NSDate (RFC2822) + (NSDateFormatter*)rfc2822Formatter { static NSDateFormatter *formatter = nil; if (formatter == nil) { formatter = [[NSDateFormatter alloc] init]; NSLocale *enUS = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]; [formatter setLocale:enUS]; [enUS release]; [formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss ZZ"]; } return formatter; } + (NSDate*)dateFromRFC2822:(NSString *)rfc2822 { NSDateFormatter *formatter = [NSDate rfc2822Formatter]; return [formatter dateFromString:rfc2822]; } @end
So we can now parse RFC2822 dates with just a single line:
#import "NSDate+RFC2822.h" ... NSDate *theDate = [NSDate dateFromRFC2822:rfc2822String]; /* autoreleased */
-
Wapi 0.2.1 and django-oauthsp 0.2 released
Sept. 10, 2008 at 09:08:24 CESTI've finally managed to solve all known issues in django-oauthsp, so it's time to release 0.2. Wapi 0.2.1 is also here, fixing a small bug in the JSON formatter and updated to match a small API change in django-oauthsp.