2

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?

Thomas Tempelmann
  • 11,045
  • 8
  • 74
  • 149

2 Answers2

3

#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().

Erol
  • 91
  • 2
  • 10
-3

Are you looking for something like this?

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

Deniz Mert Edincik
  • 4,336
  • 22
  • 24
  • Uh - my own testing shows, though, that the time is not very precise: after a real 60 seconds, this function only told me that 59s had passed (after 120s it was 118s, and so on). Sigh. Not critical for my current task but I still wonder what's going on there and why we cannot find a precise time reference on iOS. – Thomas Tempelmann Oct 26 '10 at 10:44