2011-01-25

Objective Resource and Date formatting

I've been struggling with date formatting in Objective Resource (a framework to allow Objective-C projects to work with Ruby on Rails sites). For as much as I love about this framework the date parsing is outdated.

The basic issue is that NSDateFormatter is expecting that the Z format option translates to a timezone specified like "-0800" (for example) where as my Rails 2.3.x install returns dates as "-08:00" and there is no format specifier I could find that addressed this slight difference. I was able to find this blog entry that pointed me in the right direction: http://petersteinberger.com/2010/05/nsdateformatter-and-0000-parsing/


Two parts:

Edit ObjectiveResourceDateFormatter.m in two places:

+ (NSDate *)parseDateTime:(NSString *)dateTimeString

You need to add something like this:
NSString *fixedString = [dateTimeString stringByReplacingOccurrencesOfString:@":" withString:@"" options:0 range:NSMakeRange([dateTimeString length]-5,5)];

return [formatter dateFromString:fixedString];


And then make a similar edit in:

+ (NSDate *)parseDate:(NSString *)dateString


This will allow for the correct parsing my replacing the ":" only at the end of the string. Then, you need to edit the default string format, you can do this at the top of that *.m file or you can put it in your app delegate with a line like this:

[ObjectiveResourceDateFormatter performSelector:@selector(setDateTimeZoneFormatString:) withObject:@"yyyy-MM-dd'T'HH:mm:ssZ"];


This is "cleaner" in that you aren't changing Objective Resource code directly... but I guess it's too late for that.

No comments: