繁体   English   中英

在Objective-C中计算相对时间时出现问题

[英]Problem calculating relative time in Objective-C

我有以下数据:

 ----
ORIGINAL TIME 2011-09-04 12:04:36
FORMATTED TIME 2011-09-04T12:04:36-0700
RELATIVE TIME: 2517s ago
 ----
ORIGINAL TIME 2011-09-04 11:40:17
FORMATTED TIME 2011-09-04T11:40:17-0700
 RELATIVE TIME: 1058s ago
 ----
ORIGINAL TIME 2011-09-04 08:05:00
FORMATTED TIME 2011-09-04T08:05:00-0700
RELATIVE TIME: 3h ago
----
ORIGINAL TIME 2011-09-04 07:16:00
FORMATTED TIME 2011-09-04T07:16:00-0700
RELATIVE TIME: 4h ago

我有一些代码来计算相对时间(我正在传递格式化的时间):

+(NSString*)toShortTimeIntervalString:(NSString*)sDate  
{ 
    NSDateFormatter* df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
    NSDate* date = [df dateFromString:[sDate stringByReplacingOccurrencesOfString:@"Z" withString:@"-0000"]];
    [df release];

    NSDate* today = [[NSDate alloc] init];
    NSDate *d = date; //[_twitter_dateFormatter dateFromString:sDate];
    NSTimeInterval interval = [today timeIntervalSinceDate:d];
    [today release];

    //TODO: added ABS wrapper
    double res = 0;
    NSString* result;
    if(interval > SECONDS_IN_WEEK)
    {
        res = fabs(interval / SECONDS_IN_WEEK);
        result = [NSString stringWithFormat:@"%1.0fw ago", res];
    }
    else if(interval > SECONDS_IN_DAY)
    {
        res = fabs(interval / SECONDS_IN_DAY);
        result = [NSString stringWithFormat:@"%1.0fd ago", res];
    }
    else if (interval > SECONDS_IN_HOUR){
        res = fabs(interval / SECONDS_IN_HOUR);
        result = [NSString stringWithFormat:@"%1.0fh ago", res];
    }
    else if (interval > SECONDS_IN_MIN) {
        res = fabs(interval / SECONDS_IN_MIN);
        result = [NSString stringWithFormat:@"%1.0fm ago", res];
    }
    else
    {
        interval = fabs(interval);
        result = [NSString stringWithFormat:@"%1.0fs ago", interval];
    }
    return result;
}

为什么我会在2517s ago收到,而实际上却不应将其输出为秒。 应该是41m ago

在这种情况下,间隔是-2517.0

我建议使用- (NSDateComponents *)components:(NSUInteger)unitFlags fromDate:(NSDate *)startingDate toDate:(NSDate *)resultDate options:(NSUInteger)opts方法NSCalendar获取现在和您之间的相对时间单位日期,使用类似以下内容的方法:

NSDate *dateToCompare = pointerToYourDatePulledFromTheStringRep;
NSInteger numberOfUnits;
NSString *unitString;
NSDateComponents *comps = [[NSCalendar currentCalendar] 
                          components:(NSWeekCalendarUnit | 
                                       NSDayCalendarUnit | 
                                      NSHourCalendarUnit | 
                                    NSMinuteCalendarUnit | 
                                    NSSecondCalendarUnit) 
                           fromDate:dateToCompare 
                             toDate:[NSDate date]
                            options:0];
if ([comps week] > 0) {
   numberOfUnits = [comps week];
   unitString = @"week";
} else if ([comps day] > 0) {
   numberOfUnits = [comps day];
   unitString = @"day";
} else if... // hour, minute, second

result = [NSString stringWithFormat:@"%d %@%@",
                                    numberOfUnits,
                                    unitString,
                                    (numberOfUnits != 1 ? @"s" : @"")]; 
                                    // last bit deals with plurality

这样,OS函数就可以处理所有繁琐的日期运算,并且您可以获得准确性和多日历兼容性的好处。

关于您的问题(从技术上来说),为什么秒数不是分钟数,我猜SECONDS_IN_MIN定义不正确。 但是,再次使用NSCalendar为您提供更可靠的答案。

-2517.0 -如果interval-2517.0 ,则应检查(interval < -60) 如果所有时间都过去了,则应将所有宏定义为负数,并且比较应为< ,而不是> 考虑到这一点,不确定您的小时时间是如何格式化的...

如果您的时间可能是将来或过去,则可能需要在所有比较中执行(fabs(interval) > SECONDS_IN_UNIT) (并将它们定义为肯定的)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM