parsing ISO 8601 dates

563 views
Skip to first unread message

Dennis Schmidt

unread,
Jul 13, 2011, 11:16:04 AM7/13/11
to RestKit
Following http://stackoverflow.com/questions/2201216/is-there-a-simple-way-of-converting-an-iso8601-timestamp-to-a-formatted-nsdate
there is no direct (that is, just by passing a format string) way to
parse an ISO 8601 date string like "2011-07-13T04:58:56-07:00". The
colon in the time offset seems to be the problem.

Rails (3) JSON output by default returns date strings in that format
though and I would like to parse them using RestKit.

[objectMapping.dateFormatStrings addObject:@"yyyy-MM-
dd'T'hh:mm:ssZZ"];

doesn't work for me, tested with Z, ZZ or ZZZ at the end.
So is there any way to parse that string or would I have to change my
default rails JSON output?

Rob Booth

unread,
Jul 13, 2011, 12:41:48 PM7/13/11
to res...@googlegroups.com
I've run into this in the past, before using RestKit, but I just
tested the format string I was using in the past and this works:
[mapping.dateFormatStrings addObject:@"yyyy-MM-dd HH:mm:ss'Z'"]; The
issue I'm running into with dates is that I need to include the
timezone of the dateformatter.

NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss'Z'"];
[mapping.dateFormatters addObject:dateFormatter];

Rob

I'd love to be able to just give RestKit an array of date formatters
for it to use instead of an array of strings.

Dennis Schmidt

unread,
Jul 13, 2011, 2:38:16 PM7/13/11
to RestKit
Hey Rob, thanks for the reply. Unfortunately
[mapping.dateFormatStrings addObject:@"yyyy-MM-dd HH:mm:ss'Z'"];
doesn't work for me, neither...

On 13 Jul., 18:41, Rob Booth <rob.o.bo...@gmail.com> wrote:
> I've run into this in the past, before using RestKit, but I just
> tested the format string I was using in the past and this works:
> [mapping.dateFormatStrings addObject:@"yyyy-MM-dd HH:mm:ss'Z'"];  The
> issue I'm running into with dates is that I need to include the
> timezone of the dateformatter.
>
> NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
> [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
> [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss'Z'"];
> [mapping.dateFormatters addObject:dateFormatter];
>
> Rob
>
> I'd love to be able to just give RestKit an array of date formatters
> for it to use instead of an array of strings.
>
> On Wed, Jul 13, 2011 at 8:16 AM, Dennis Schmidt
>
>
>
>
>
>
>
> <metzelti...@googlemail.com> wrote:
> > Followinghttp://stackoverflow.com/questions/2201216/is-there-a-simple-way-of-c...

Victor Kryukov

unread,
Jul 17, 2011, 2:25:56 AM7/17/11
to res...@googlegroups.com
The problem is indeed with NSDateFormatter - it parses everything according to http://unicode.org/reports/tr35/tr35-10.html#Date_Format_Patterns, so Z at the end means 'parse timezone according to RFC 822' which doesn't allow for colons.

The following quick patch (warning: not tested!) will extend RestKit to allow not just NSStrings (which will be feed to NSDateFormatter's setDateFormat:) but also _any_ object that responds to @selector(dateFromString:) as date formatters.


So you should be able to just add your own parser, and everything should work.

@class MyISO8601DateFormatter; // implements dateFromString: to parse ISO 8601 dates
[mapping.dateFormatStrings addObject:[[[MyISO8601DateFormatter] alloc] init] autorelease]];


Blake Watters

unread,
Jul 20, 2011, 10:43:38 PM7/20/11
to res...@googlegroups.com
We have been moved to an array of date formatters on the GateGuru fork as a performance optimization. If someone wants to extract it or send a pull request to improve the situation on this, I'll merge it.

Kyle Clegg

unread,
Jul 30, 2012, 6:46:14 PM7/30/12
to res...@googlegroups.com
Is there still a working github link out there?  I'm in need of a fix for this same problem.

Kyle Clegg

unread,
Aug 1, 2012, 11:59:06 AM8/1/12
to res...@googlegroups.com
Decent documentation on this now exists here: https://github.com/RestKit/RestKit/wiki/Configuring-Date-and-Time-Mapping
Reply all
Reply to author
Forward
0 new messages