2

a relatively simple question that I've not been able to find a clear answer to. My app is more complex, but answering this question will suffice.

Suppose you're writing a stopwatch app. When the user taps "start", the app stores the current date and time in startTime:

startTime = [NSDate date];

When the user tapes "stop", the app stores the current date and time in stopTime:

stopTime = [NSDate date];

The duration is calculated by:

duration = [stopTime timeIntervalSinceDate:startTime];

and is displayed with something like:

[durationLabel setText:[NSString stringWithFormat:@"%1.2f", duration]];

The typical durations that my app is timing range from 2 to 50 seconds. I need accuracy to 1/100th of a second (e.g. 2.86 seconds).

I'm assuming that there is some protocol that iOS devices use to keep their clocks accurate (cellular or NTP sources?). My concern is that between starting and stopping the stopwatch, the clock on the iOS device is updated which can result in a shift of the current date/time either ahead or back. If this were to happen, the duration calculated would be inaccurate.

I've seen a few posts relating to timing methods for purposes of improving code efficiency. Some suggest using mach_time.h functions, which I'm not familiar with. It's not obvious to me which is the best approach to use.

Is it possible to disable iOS from updating the date & time? Is mach_absolute_time() unaffected by iOS clock updates?

Many thanks!

Tim

Tommy Devoy
  • 13,441
  • 3
  • 48
  • 75
Tim123
  • 33
  • 1
  • 4

2 Answers2

1

You are correct in thinking that CFAbsoluteTime and its derivatives (NSDate dateand so on) are potentially skewed by network updates on 'real' time. Add that to the fact that NSTimer has an accuracy of 50-100ms and you have a timer that is not suited to the most critical of time-sensitive operations.

The answer to this problem seems to be CACurrentMediaTime. It is a member of the Core Animation group, but there shouldn't be any problem integrating it into non-animation based applications.

CACurrentMediaTime is a wrapper of mach_absolute_time() and makes sense of the "mach absolute time unit," which from my understanding is no fun to tinker with. mach_absolute_time() is calculated by running a non-network synced timer since the device was last booted.

There is relatively little information on CACurrentMediaTime but here are some sources and further reading:

Note: If you do use CACurrentMediaTime, make sure you include and link the QuartzCore.framework

Community
  • 1
  • 1
user
  • 3,388
  • 7
  • 33
  • 67
0

Check out this here. I would say forget about the current time check and use a precision timer since it won't rely on the current time but instead uses an interval.

Mark McCorkle
  • 9,349
  • 2
  • 32
  • 42
  • Thanks for the link to the document. It seems more about timers (e.g. invoking a method at some precise time in the future). For my stopwatch-like app, I just want to note a start time (user taps a button), a stop time (also user taps a button), then calculate and display the duration. No need for NSTimer or the more precise timing approaches described in the document. But I'm glad to know about this for future needs, so thanks again for your reply. – Tim123 Jul 06 '13 at 05:37