手头的任务非常简单 - 将NSTimeInterval转换为看起来像12:34:56的NSString(12小时34分56秒)。
答案 0 :(得分:10)
这是@ golegka答案的更快版本:
NSString *stringFromInterval(NSTimeInterval timeInterval)
{
#define SECONDS_PER_MINUTE (60)
#define MINUTES_PER_HOUR (60)
#define SECONDS_PER_HOUR (SECONDS_PER_MINUTE * MINUTES_PER_HOUR)
#define HOURS_PER_DAY (24)
// convert the time to an integer, as we don't need double precision, and we do need to use the modulous operator
int ti = round(timeInterval);
return [NSString stringWithFormat:@"%.2d:%.2d:%.2d", (ti / SECONDS_PER_HOUR) % HOURS_PER_DAY, (ti / SECONDS_PER_MINUTE) % MINUTES_PER_HOUR, ti % SECONDS_PER_MINUTE];
#undef SECONDS_PER_MINUTE
#undef MINUTES_PER_HOUR
#undef SECONDS_PER_HOUR
#undef HOURS_PER_DAY
}
这并不是NSCalendars
,它只是使用直线整数数学。