tags:

views:

748

answers:

3

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

A: 

File a feature request with Apple and let them know you want this functionality on the iPhone. NSDateFormatter provides a legacy init method that takes a boolean flag indicating you want it to parse natural language, but it's only available on OS X. Wil Shipley wrote an interesting post a while back about this functionality in the context of heuristics and human factors.

It doesn't seem likely that Apple will provide this functionality as this note would indicate from the NSDateFormatter docs:

iPhone OS Note: iPhone OS supports only the 10.4+ behavior. 10.0-style methods and format strings are not available on iPhone OS.

In other words, I think you'll have to parse it yourself.

Matt Long
A: 

Sounds like you need something like: ISO 8601 parser and unparser.

Ramin
The format of the example provided in the question is not ISO 8601.
Peter Hosey
+2  A: 

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 +0000 yyyy"];

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

NSTimeInterval seconds = [date timeIntervalSince1970];
John Fricker