使用 NSDateFormatter 将 NSString 转换为 unix 时间——代码在 50% 的时间内有效

发布于 2024-09-17 23:50:00 字数 2132 浏览 5 评论 0原文

这真的很奇怪,我不确定是什么导致代码一半时间完美工作,而另一半时间则不然。我正在使用 TBXML 来解析 Twitter 提要。我需要获取 twitter feed 中的created_at日期,该日期的传递方式如下:Tue Feb 02 23:30:49 +0000 2010。因此,我存储这些值,然后将它们每个传递给我的 getTweetTime 方法,该方法将格式化它们,让它们进行 unix,然后与当前日期进行比较,以返回类似于 twitters 的字符串“大约 3 小时前”日期结构。

问题在于,即使所有值都正确存储并使用正确的值传递给我的 getTweetTime 方法,但其中许多值并未使用合法值完成格式化步骤。这是我的代码:

    -(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)"];
    }    

}

每次成功解析的内容之间似乎没有任何模式。这是一个典型的控制台输出,1 次成功,1 次失败,其中 time=0 表示失败:

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

我尝试在字符串中添加多个“Z”,但无济于事。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄居人 2024-09-24 23:50:00

尝试使用大写 HH 来表示小时。标准规定“h”是从 1 到 12 的小时。您会注意到您的示例“失败”的时间为 17 小时。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文