Reputation: 934
I'm trying to convert a string from a Twitter API result into an NSDate object using NSDateFormatter. What I'm finding is that the NSDateFormatter is just returning the current date/time of my machine instead. Any help would be greatly appreciated!
Here's what the string looks like coming into my function:
Sat, 11 Dec 2010 01:35:52 +0000
And here's the function:
+(NSDate*)convertTwitterDateToNSDate:(NSString*)created_at
{
// Sat, 11 Dec 2010 01:35:52 +0000
static NSDateFormatter* df = nil;
df = [[NSDateFormatter alloc] init];
[df setTimeStyle:NSDateFormatterFullStyle];
[df setFormatterBehavior:NSDateFormatterBehavior10_4];
[df setDateFormat:@"EEE, d LLL yyyy HH:mm:ss Z"];
NSDate* convertedDate = [df dateFromString:created_at];
[df release];
return convertedDate;
}
And here's the date returned:
2010-12-10 17:58:26 -0800
Which happens to be the date and time of my computer right now.
*Note!* changed memory management from using an autorelease when allocating the DateFormater, to an explicit release in the function. When calling this in a giant loop, I found that it started failing with the autorelease (I'm guessing the auto part wasn't releasing fast enough), but changing it to this fixed the problem.
Upvotes: 3
Views: 3607
Reputation: 45118
I think is right. because the original string is in +0000 time zone and the result date is in -0800 time zone. So if you lookt at them carefully you will notice is the same time.
When you print a date like [date description] it will be printed in the local time zone, I believe but the time is absolute. Since NSDate contains only seconds and not calendars, not time zone info.
(printed string is relative to local time zone but the real date is absolute)
Upvotes: 1