iPhone + Twitter API: Converting time?

后端 未结 4 905
旧时难觅i
旧时难觅i 2020-12-30 09:14

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

相关标签:
4条回答
  • 2020-12-30 09:42

    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];
    
    0 讨论(0)
  • 2020-12-30 09:42

    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.

    0 讨论(0)
  • 2020-12-30 09:47

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

    0 讨论(0)
  • 2020-12-30 10:07

    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.

    0 讨论(0)
提交回复
热议问题