views:

644

answers:

3

Hi,

I need to ensure that my app gets the correct time, not the time set by the user in settings.

So for instance, once the user starts the app, I'd want to get a timestamp. The user would see the elapsed time on a timer on screen updated every second. When the user exits the app, and after some time restarts the app , the timer would just get the current time and compare it with the original timestamp it kept. So basically, just like the stopwatch in the Clock.App on iPhone.

Now the problem with the clock.app is that if the user goes and advances the time in Settings by one hour, this will influence the timer. Obviously I don't want that, because in my app, that would be cheating.

I'd need to get a more trustworthy time source to compare to.

I could get the time from the internet. But the problem is that I'd need internet connection , so only works on if there is an internet connection. My app needs to work offline preferably.

Is there some kind of internal clock I can access?

Is there a way of checking whether the user has changed the date time in Settings?

Any other ideas?

Thanks

A: 

Try 'CFAbsoluteTimeGetCurrent'

From the docs:

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.

cmaughan
Can't have a solution that would be influenced by the change of the clock.
Maxm007
-1: Doesn't answer the question
Casebash
+1  A: 

Get the time from NIST with the Daytime Protocol:

UDP Based Daytime Service A server listens for UDP datagrams on UDP port 13. When a datagram is received, an answering datagram is sent containing the current date and time as a ASCII character string (the data in the received datagram is ignored).

NIST Format of response: JJJJJ YR-MO-DA HH:MM:SS TT L H msADV UTC(NIST) OTM

http://tf.nist.gov/service/its.htm

http://tf.nist.gov/tf-cgi/servers.cgi

zaph
Hi, can't use internet time because my app needs to work offline.
Maxm007
+1  A: 

The best solution I've come up with is using the mach timer which counts time units since last iphone boot.

This works great. The only restriction is that the user cannot be allowed to reboot or it would invalidate his time.

I detect a reboot by initially storing the iphone timestamp associated with the mach timer and then checking every time the app starts, so it hasn't changed. This has as a side effect that if the user changes iPhone DateTime while he's being timed , that will also invalidate the score, but that is ok.

http://stackoverflow.com/questions/1443601/how-can-i-detect-whether-the-iphone-has-been-rebooted-since-last-time-app-started

I can easily warn my users about this: rebooting or changing iphone time while you're on the clock will invalidate your scored time.

Maxm007