0

I am having an issue trying to parse an Atom RFC 3339 date coming from a feed. Is is coming in is form: @"2014-07-21T11:36:05-05:00" and the following formats fail to parse it... any help?

        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"]; 

        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"]; 

        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"]; 

        [dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.ZZZZ"];
jscs
  • 63,694
  • 13
  • 151
  • 195
izk
  • 1,189
  • 2
  • 8
  • 23
  • 1
    Use a format that actually matches your string. Everything is right up to and include the `ss` but then you get it wrong for the timezone. Your first option is closest but you need five `Z`s for a timezone in that format. – rmaddy Jul 21 '14 at 19:30
  • 1
    BTW - get rid of all of the ticks except around the "T". – rmaddy Jul 21 '14 at 19:49
  • see comment below please – izk Jul 21 '14 at 20:24
  • What version of iOS are you testing on? – rmaddy Jul 21 '14 at 20:42
  • Actually pasting the code in a fresh project seems to work. The issue is somewhere else it seems... thanks! – izk Jul 21 '14 at 20:46
  • Why do you have a `.` in the last format?? I don't see one in your source string. – Hot Licks Jul 21 '14 at 20:59
  • The issue is resolved, but to answer the question, all those formats are chained in the code because different RSS feeds use them. If one fails, the next one tries. I was just showing all the formats I was using at the moment of the bug. Clearly I am not fully familiar with NSDateFormatter and don't always know if a character is special or literal. – izk Jul 22 '14 at 21:42

1 Answers1

6

The format you need is

yyyy-MM-dd'T'HH:mm:ssZZZZZ

The five Z pattern is a recent-ish addition to the formatting library that handles exactly this time zone format.

(Despite Apple's Technical Q&A 1480 on this topic (and the identical code in the date formatting guide), the only item in the format string that needs to be escaped with apostrophes is the T -- Unicode Technical Standard #35, which governs the format strings, says that only ASCII letters, a-z and A-Z, are reserved. It also says, however, that punctuation may be used in the future, so you can quote the colons and hyphens if you like.)

jscs
  • 63,694
  • 13
  • 151
  • 195