当前位置:编程学习 > wap >>

iPhone开发--获取当前时间与2001年1月1日的时间差以毫秒为单位

因为程序的需要,要计算两个时间的时间间隔,既时间差,如果用NSDate等方法拿到的时间格式很难进行差值运算(个人觉得),于是想有没有一个方法象java中的那样获取1970年以后的时间,上伟大的stackOverFlow上查找答案 终于找到了,
 
原文翻译如下:
 
看CFAbsoluteTimeGetCurrent :http://developer.apple.com/documentation/CoreFoundation/Reference/CFTimeUtils/index.html
 
 
[cpp] CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
 // do something you want to measure 
 CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
 NSLog(@"operation took %2.5f seconds", end-start);
 CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
 // do something you want to measure
 CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
 NSLog(@"operation took %2.5f seconds", end-start);
 如果你觉得CFAbsouteTime 是冗长的,你可以用简单的double方法替代它。
 
因为在CFDate.h中是如此定义的。 www.zzzyk.com
 
 
[cpp] <span style="font-family:'Times New Roman';">typedef double CFTimeInterval;
 typedef CFTimeInterval CFAbsoluteTime;</span>
 <span style="font-family:'Times New Roman';">typedef double CFTimeInterval;
 typedef CFTimeInterval CFAbsoluteTime;</span>
 
追加:
 
实际上NSDate自带了获取自1970年到now的毫秒数,只是自己浅薄,没找到而已,呵呵:
 
[cpp] [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]
 [NSString stringWithFormat:@"%d", (int)[[NSDate date] timeIntervalSince1970]]曾经自己还傻傻的用java算1970年到2001年的毫秒数,再加上now到2001年的毫秒数。呵呵。
 
 摘自zcl369369的专栏

补充:移动开发 , IOS ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,