关于iOS中的时间

时间:2020-12-31 07:37:39

两类

  1. 绝对时间
    [NSDate date]CFAbsoluteTimeGetCurrent(),或者gettimeofday()
    返回的是从某一个时刻开始,度过的秒数。会随着用户设置的系统时间更改。
  2. 相对时间
    CACurrentMediaTime 或者-[NSProcessInfo systemUptime],相对时间。重启之后,会重新计数。

本质区别:
NSDate 或 CFAbsoluteTimeGetCurrent() 返回的时钟时间将会会网络时间同步,从时钟 偏移量的角度,mach_absolute_time() 和 CACurrentMediaTime() 是基于内建时钟的,能够更精确更原子化地测量,并且不会因为外部时间变化而变化(例如时区变化、夏时制、秒突变等),但它和系统的uptime有关,系统重启后CACurrentMediaTime()会被重置。

参考资料

  1. Keeping Track of Time Passed on iOS
  2. NSDate 、CFAbsoluteTimeGetCurrent、CACurrentMediaTime 的区别
  3. Benchmarking

相关文章