Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter off by one hour

I'm trying to format a date from a RSS feed:

Mon, 27 Feb 2012 10:33:00 EDT (format from the RSS feed)

To:

Mon, 27 Feb 2012 10:33 AM

Here's what i'm doing in code:

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d MM yyyy HH:mm:ss zzz"];  

NSDate *date = [dateFormat dateFromString:@"Mon, 27 Feb 2012 10:33:00 EDT"];

[dateFormat setDateFormat:@"EE, d MMM yyyy hh:mm a"];

formatedDateLabel.text = [dateFormat stringFromDate:date];

[dateFormat release];

However this always outputs:

Mon, 27 Feb 2012 9:33 AM

Note that my devices is set to EST. I've tried adjusting the date formatter to match EDT but that doesn't seem to make any difference.

Any ideas on what i'm doing wrong?

like image 777
James Jones Avatar asked Nov 04 '22 04:11

James Jones


1 Answers

You're not doing anything wrong. The reason this is happening is because NSTimezone's internal representation of EDT and EST are identical:

// These objects are the same:
[NSTimeZone timeZoneWithAbbreviation:@"EST"];
[NSTimeZone timeZoneWithAbbreviation:@"EDT"];

NSDateFormatter dateFromString: is smart enough to know that a date from February marked EDT should have a GMT offset of -4 instead of -5, even though that's not a well-formed date. But when you use it to display dates, if its timezone is set to EST or EDT (which it treats as the same thing), it will use the appropriate timezone based on whether daylight savings is in effect at the time.

like image 194
yuji Avatar answered Nov 15 '22 04:11

yuji