cocoa - Parsing rfc3339 dates with NSDateFormatter in iOS 4.x and MacOS X 10.6: impossible? -


parsing rfc3339 date nsdateformatter appears impossible, in general case. wrong? [edit 2 years later: there way! see below , footnote.]

a not-especially-malleable web service feeding me dates like:

2009-12-31t00:00:00-06:00 

rfc3339 compliant, default output of jaxb library they're using. note colon, rfc3339 requires when offset isn't literal "z":

time-numoffset  = ("+" / "-") time-hour ":" time-minute time-offset     = "z" / time-numoffset 

i want parse these nsdates.

nsdateformatter wants patterns in syntax specified unicode, offers date field symbols timezones "pdt", "-0800", "gmt-08:00" not "-08:00".

googling, , other similar questions, produces date formats like

[mydateparser setdateformat:@"yyyy'-'mm'-'dd't'hh':'mm':'ssz"]; /* or: */ [mydateparser setdateformat:@"yyyy'-'mm'-'dd't'hh':'mm':'ss'z'"]; 

the latter of requires literal "z", , former insists either absence of colon or presence of "gmt". however, appeared work before ios 4.x (possibly discarding tz offset completely; data aren't clear.)

my options @ point sorry lot:

  • discover undocumented format specifier, or strange mode put nsdateformatter into, accept stray colon: longshot, nonexistent. [footnote]
  • persuade service publisher turn dates zulu time , specify 'z': politically challenging.
  • write own nsformatter subclass or research old strptime_l: work. :)
  • string-manipulate input , strip last colon: brittle , ugly, path of least resistance.

have understood situation accurately, current nsdateformatter follows unicode strictly without extensions; , unicode formats insufficient describe rfc3339 date?

[footnote] come 3 years later tack on small addendum: unicode , apple have added feature format strings, of ios6/osx10.8. compare the latest revision of writing its immediate predecessor, , note addition of 5 "z"s, yields zone format "-08:00". if can away ditching support 5.x/10.7, there's new right way it. i'll leave previous answer stand, it's still best approach when backward compatibility required.

date string parsing in cocoa can pain, if have deal dates generated .net based web services.

i'd suggest looking @ nsdate+internetdatetime category michael waterfall has on nsdate part of mwfeedparser project on github. it's worked me parsing format of date describe.

https://github.com/mwaterfall/mwfeedparser/


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -