Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
I'm using a class of NSDate+InternetTime to help with parsing XMLs for Calendars, Blogs, etc. I am in Central Time Zone and when I view the TableView showing each RSS Item, the time listed is correct. However, if a user is in EST it shows an hour later, and earlier times for Mountain and Pacific Time Zones. Any ideas why this may be? The code for parsing the date is:

Code:
NSString *articleDateString = [item valueForChild:@"updated"];        
        NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC3339];

The code in the class NSDate+InternetTime is:
Code:
+ (NSDate *)dateFromInternetDateTimeString:(NSString *)dateString formatHint:(DateFormatHint)hint {
	NSDate *date = nil;
	if (hint != DateFormatHintRFC3339) {
		// Try RFC822 first
		date = [NSDate dateFromRFC822String:dateString];
		if (!date) date = [NSDate dateFromRFC3339String:dateString];
	} else {
		// Try RFC3339 first
		date = [NSDate dateFromRFC3339String:dateString];
		if (!date) date = [NSDate dateFromRFC822String:dateString];
	}
	return date;
}
&
Code:
+ (NSDate *)dateFromRFC3339String:(NSString *)dateString {
	
	// Create date formatter
	static NSDateFormatter *dateFormatter = nil;
	if (!dateFormatter) {
		NSLocale *en_US_POSIX = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
		dateFormatter = [[NSDateFormatter alloc] init];
		[dateFormatter setLocale:en_US_POSIX];
		[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
		[en_US_POSIX release];
	}
	
	// Process date
	NSDate *date = nil;
	NSString *RFC3339String = [[NSString stringWithString:dateString] uppercaseString];
	RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"];
	// Remove colon in timezone as iOS 4+ NSDateFormatter breaks. See https://devforums.apple.com/thread/45837
	if (RFC3339String.length > 20) {
		RFC3339String = [RFC3339String stringByReplacingOccurrencesOfString:@":" 
																 withString:@"" 
																	options:0
																	  range:NSMakeRange(20, RFC3339String.length-20)];
	}
	if (!date) { // 1996-12-19T16:39:57-0800
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ssZZZ"]; 
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) { // 1937-01-01T12:00:27.87+0020
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss.SSSZZZ"]; 
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) { // 1937-01-01T12:00:27
		[dateFormatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss"]; 
		date = [dateFormatter dateFromString:RFC3339String];
	}
	if (!date) NSLog(@"Could not parse RFC3339 date: \"%@\" Possibly invalid format.", dateString);
	return date;
	
}
I would love to have it where it shows the same time for all time zones. I could then put in the app that all times for the calendar events are in EST or whatever. Any thoughts?

UPDATE:
In the cellForRowAtIndexPath of the TableView I had:
Code:
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    [dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
    NSString *articleDateString = [dateFormatter stringFromDate:entry.articleDate];
    cell.textLabel.text = entry.articleTitle; 
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", articleDateString, entry.blogTitle];
I added in where the blank line is:
Code:
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
I tried messing with this to all sorts of different time zones, and it never changed, yet if I changed the time zone of my phone, the times would change.
 
Last edited:
Or let me ask this way. Is it possible to use dateFormatter to take the time being returned, and either add or subtract hours to it?
 
Dates are a pain.

To add or subtract time use NSDateComponents. I guess you want to do that to make a +1 adjustment, which is probably a bad idea. You should figure out what the problem is.

It seems that your code is trying to format a date at GMT, not local time. Although I'm not sure.

You also might post this question on the Core sub forum at Apple's iOS forum, where some people are more knowledgable about this topic.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.