iOS: How to measure passed time, independent of clock and time zone changes?

不羁的心 提交于 2019-12-01 12:17:38

问题


In order to measure the time duration while my app is running, as well as the time that passed while my app was idle in the background, I need a reference clock that is not altered by the user changing the Time+Date of his calendar clock.

I can not rely on NSDate because that can be changed by the user while my app is in the background (and no, intercepting the notifications related to such clock changes is overkill for my needs).

What I need is a function such as "seconds since boot of the OS". Mac OS offer such functions (both Ticks and Microseconds), but what about iOS?


回答1:


#include <mach/mach_time.h> into your source file.

replace any calls to clock_gettime(CLOCK_MONOTONIC, &timespec_t) with calls to mach_absolute_time().




回答2:


Are you looking for something like this?

http://www.wand.net.nz/~smr26/wordpress/2009/01/19/monotonic-time-in-mac-os-x/



来源:https://stackoverflow.com/questions/4008503/ios-how-to-measure-passed-time-independent-of-clock-and-time-zone-changes

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!