gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:10:28 25 4
gpt4 key购买 nike

这真的很奇怪,我不确定是什么导致代码一半时间完美工作,而另一半时间则不然。我正在使用 TBXML 来解析 twitter feed。我需要获取 Twitter feed 中的created_at 日期,该日期的传递方式如下 <created_at>Tue Feb 02 23:30:49 +0000 2010</created_at> 。所以我存储这些值,然后将它们每个传递给我的 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”,但没有成功。

最佳答案

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

关于iphone - 使用 NSDateFormatter 将 NSString 转换为 unix 时间——代码在 50% 的时间内有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3631515/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com