views:

63

answers:

1

This is really strange, I'm not sure what could be causing the code to work perfectly half of the time and not the other half. I'm using TBXML to parse a twitter feed. I need to get the created_at date in the twitter feed which is passed like <created_at>Tue Feb 02 23:30:49 +0000 2010</created_at>. So i store these values and then pass each of them to my getTweetTime method, which will format them, get them to unix them, and then compare against the current date in order to return a string similar to twitters "about 3 hours ago" date structure.

The problem is that even though the values are all being stored correctly and passed to my getTweetTime method with the correct values, many of them don't make it through the formatting step with a legitimate value. Heres my code:

    -(NSString *) getTweetTime:(NSString*)time
{       
    NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
    [df setDateFormat:@"ccc MMM dd hh:mm:ss Z yyyy"];
    NSDate *myDate = [df dateFromString: time];


    NSLog(@"time%@", time);
    long tweetTime =  (long)[myDate timeIntervalSince1970];
    long currentTime = (long)[[NSDate date] timeIntervalSince1970]; 

    int delta = currentTime - tweetTime;    
    NSLog(@"tweet:%ld, current:%ld", tweetTime, currentTime);

    if (delta < 60) {
        return @" (less than a minute ago)";
    } else if (delta < 120) {
        return @" (about a minute ago)";
    } else if (delta < (60 * 60)) {
        return [[NSString stringWithFormat:@" ( about %u",(delta / 60)] stringByAppendingString:@" minutes ago)"];
    } else if (delta < (120 * 60)) {
        return @" (about an hour ago)";
    } else if (delta < (24 * 60 * 60)) {        
        return [[NSString stringWithFormat:@" (about %u",((delta / 3600) - 1)] stringByAppendingString:@" hours ago)"];
    } else if (delta < (48 * 60 * 60)) {
        return @" (1 day ago)";
    } 
    else if(tweetTime == 0){return @"********";}
    else {
        return [[NSString stringWithFormat:@" (%u",(delta / 86400)] stringByAppendingString:@" days ago)"];
    }    

}

There doesn't seem to be any pattern between whats successfully parsed each time. Here's a typical console output of 1 success, 1 failure, where time=0 is a fail:

2010-09-02 14:45:12.963 myApp[6401:307] timeTue Aug 31 17:59:34 +0000 2010
2010-09-02 14:45:12.964 myApp[6401:307] tweet:0, current:1283463912
2010-09-02 14:45:12.970 myApp[6401:307] timeTue Aug 31 06:36:29 +0000 2010
2010-09-02 14:45:12.972 myApp[6401:307] tweet:1283236589, current:1283463912

I've tried adding a number of 'Z's to the string as well to no avail.

A: 

Try upper case HH for the hour. The standard says 'h' is hour from 1-12. You'll note your example that "fails" has an hour of 17.

imaginaryboy
Thanks, you got it.
culov