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:
The code in the class NSDate+InternetTime is:
&
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:
I added in where the blank line is:
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.
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;
}
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];
Code:
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"EST"]];
Last edited: