我正在将字符串日期/时间转换为数字时间值。在我的情况下,我只是用它来确定某个东西是否比其他东西新/旧,所以这个小十进制问题不是一个真正的问题。它不需要精确到秒。但它仍然让我摸不着头脑,我想知道为什么..
我的日期采用字符串格式 @"2010-09-08T17:33:53+0000"。所以我写了这个小方法来返回一个时间值。在有人跳到 28 天或 31 天的月份中有多少秒之前,我不在乎。在我的数学中,可以假设所有月份都有 31 天,而年份有 31*12 天,因为我不需要两个时间点之间的差异,只知道一个时间点是否晚于另一个时间点。
-(float) uniqueTimeFromCreatedTime: (NSString *)created_time {
float time;
if ([created_time length]>19) {
time = ([[created_time substringWithRange:NSMakeRange(2, 2)]floatValue]-10) * 535680; // max for 12 months is 535680.. uh oh y2100 bug!
time=time + [[created_time substringWithRange:NSMakeRange(5, 2)]floatValue] * 44640; // to make it easy and since it doesn't matter we assume 31 days
time=time + [[created_time substringWithRange:NSMakeRange(8, 2)]floatValue] * 1440;
time=time + [[created_time substringWithRange:NSMakeRange(11, 2)]floatValue] * 60;
time=time + [[created_time substringWithRange:NSMakeRange(14, 2)]floatValue];
time = time + [[created_time substringWithRange:NSMakeRange(17, 2)]floatValue] * .01;
return time;
}
else {
//NSLog(@"error - time string not long enough");
return 0.0;
}
}
当传递上面列出的那个字符串时,结果应该是 414333.53,但它返回的是 414333.531250。
当我在每次=之间扔一个 NSLog 来跟踪它在哪里发生时,我得到了这个结果:
time 0.000000
time 401760.000000
time 413280.000000
time 414300.000000
time 414333.000000
floatvalue 53.000000
time 414333.531250
Created Time: 2010-09-08T17:33:53+0000 414333.531250
所以最后一个 floatValue 返回 53.0000,但是当我将它乘以 0.01 时,它变成了 .53125。我也尝试了 intValue,它做了同样的事情。