Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone + Twitter API: Converting time?

Tags:

iphone

twitter

Is there an easy way to convert the time stamp you get from twitter into unix time or minutes since now? I could parse through the string and convert everything myself but I'm hoping there is a way to convert that doesn't need that. Here is an example of a created_at element with a time stamp.

Sun Mar 18 06:42:26 +0000 2007

like image 956
TheGambler Avatar asked Jan 04 '10 21:01

TheGambler


2 Answers

You can use NSDateFormatter with something like this :


NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
[dateFormatter setLocale:usLocale]; 
[usLocale release];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
[dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];

// see http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
[dateFormatter setDateFormat: @"EEE MMM dd HH:mm:ss Z yyyy"];

NSDate *date = [dateFormatter dateFromString:[currentDict objectForKey:@"created_at"]];
[dateFormatter release];

NSTimeInterval seconds = [date timeIntervalSince1970];
like image 135
John Fricker Avatar answered Nov 10 '22 20:11

John Fricker


I have been strugeling with this all day, but this thread helped me to find a solution.

This is how I convert the Twitter "created_at" attribute to a NSDATE;

NSDateFormatter *fromTwitter = [[NSDateFormatter alloc] init];
// here we set the DateFormat  - note the quotes around +0000
[fromTwitter setDateFormat:@"EEE MMM dd HH:mm:ss '+0000' yyyy"];
// We need to set the locale to english - since the day- and month-names are in english
[fromTwitter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en-US"]];

NSString *dateString = [item objectForKey:@"created_at"];
NSDate *tweetedDate = [fromTwitter dateFromString:dateString];

I hope someone will find this helpful.

like image 32
HonkyHonk Avatar answered Nov 10 '22 20:11

HonkyHonk