NSDate objects encapsulate a single point in time,
independent of any particular calendrical system or time zone.
Date objects are immutable, representing an invariant time interval
relative to an absolute reference date (00:00:00 UTC on 1 January 2001).
NSDate对象描述的是时间线上的一个绝对的值,和时区和文化无关,它参考的值是:以UTC为标准的,2001年一月一日00:00:00这一刻的时间绝对值。
这里有个概念很重要,我们用编程语言描述时间的时候,都是以一个时间线上的绝对值为参考点,参考点再加上偏移量(以秒或者毫秒,微秒,纳秒为单位)来描述另外的时间点。
理解了这一点,再看NSDate的一些API调用就非常清楚了,比如:
NSDate* date = [NSDate date];
NSLog(@"current date interval: %f", [date timeIntervalSinceReferenceDate]);
timeIntervalSinceReferenceDate返回的是距离参考时间的偏移量,这个偏移量的值为502945767秒,502945767/86400/365=15.9483056507,86400是一天所包含的秒数,365大致是一年的天数,15.94当然就是年数了,算出来刚好是此刻距离2001年的差值。
又比如,此刻我写文章的时候,当前时间为北京时间上午11:29,看看下面代码的输出:
NSDate* date = [NSDate date];
NSLog(@"current date: %@", date);
current date: 2016-12-09 03:29:09 +0000。可见NSDate输出的是绝对的UTC时间,而北京时间的时区为UTC+8,上面的输出+8个小时,刚好就是我当前的时间了。
NSDate和市区和文化无关,所以要展示具体格式的时间,我们需要NSDateFormatter和NSTimeZone的辅助。
另外关于NSDate最重要的一点是:NSDate是受手机系统时间控制的。也就是说,当你修改了手机上的时间显示,NSDate获取当前时间的输出也会随之改变。在我们做App的时候,明白这一点,就知道NSDate并不可靠,因为用户可能会修改它的值。
CFAbsoluteTimeGetCurrent()
官方定义如下:
Absolute time is measured in seconds relative to the absolute reference date of Jan 1 2001 00:00:00 GMT.
A positive value represents a date after the reference date, a negative value represents a date before it.
For example, the absolute time -32940326 is equivalent to December 16th, 1999 at 17:54:34.
Repeated calls to this function do not guarantee monotonically increasing results.
The system time may decrease due to synchronization with external time references or due to an explicit
user change of the clock.
从上面的描述不难看出CFAbsoluteTimeGetCurrent()的概念和NSDate非常相似,只不过参考点是:以GMT为标准的,2001年一月一日00:00:00这一刻的时间绝对值。
同样CFAbsoluteTimeGetCurrent()也会跟着当前设备的系统时间一起变化,也可能会被用户修改。










