18

I am looking for a fully working solution, one that works with:

  • any iOS locale or timezone
  • any/most HTTP servers
  • Xcode 4.0.2 (see why)

Current broken code:

 NSString lastModifiedString = @"Mon, 06 Jun 2011 12:47:05 GMT";
 NSDateFormatter *df = [[NSDateFormatter alloc] init];  
 //df.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";
 df.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss z";
 df.locale = [[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease];  
 df.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];  
 NSDate date = [df dateFromString:lastModifiedString];  

I assumed that Last-Modified is supposed to use the same format like other date fields in HTTP spec, meaning to use RFC-1123 / RFC-822

Resources:

Daniel Galasko
  • 23,617
  • 8
  • 77
  • 97
sorin
  • 161,544
  • 178
  • 535
  • 806
  • @karim this was long time ago and solved (i don't remember exactly how) but I do not see the point on your comment. – sorin Jun 28 '12 at 10:02
  • Thanks for the solution. It helps me. I was just comparing with "Last-Modified" header and the date formatter string (pasting the header here). – karim Jun 28 '12 at 10:30

1 Answers1

7

The only thing wrong is that you're missing asterisks in-between NSString and lastModifiedString, and also between NSDate and date. Once you put those in, this code works for me.

Dave DeLong
  • 242,470
  • 58
  • 448
  • 498