9

I'm looking for a reliable way to get the time. It can't be tampered with and it needs to work offline. So no internet time , no user time setup in settings and no BSD uptime time since last reboot. I was wondering since GPS works using atomic clock, whether I could access that information.

Thank you

4
  • You know that the iPhone shuts off GPS in "airplane mode", right? Also, there are a lot of places where you won't get a GPS signal, so even if you could access the time component (which I would expect is not available through an API), you can't be guaranteed of its availability.
    – kdgregory
    Sep 18, 2009 at 13:11
  • That's OK. If GPS is not getting signal that's fine. I can enforce that I only need to sample untampered time in locations where I've tested for GPS availability before.
    – Maxm007
    Sep 18, 2009 at 13:13
  • 1
    I would even find it acceptable to get carrier time, but that doesn't seem to be available in the API either.
    – Maxm007
    Sep 18, 2009 at 13:14
  • You say, "If the phone uses GPS as a time sync source". Does it ever sync to GPS time? How do you know?
    – user565139
    Jan 6, 2011 at 7:41

5 Answers 5

7

This works to get the GPS time:

#import <CoreLocation/CoreLocation.h>

CLLocation* gps = [[CLLocation alloc]
                       initWithLatitude:(CLLocationDegrees) 0.0
                       longitude:(CLLocationDegrees) 0.0];
NSDate* now = gps.timestamp;

It doesn't seem to be tamper-proof though.

I tried this code on an iPhone 4 in airplane mode (iOS 6.1), and even then it gives a time all right. But unfortunately this time seems to change with the system clock. Ugh.

Funny thing that I found (still in airplane mode) is that if you tamper with the system clock (after turning to off Time & Date's Set Automatically), and then turn Set Automatically back to on, the machine restores the real (original) time without a hitch. this works even after cycling the phone's power. So it seems that there is something like a tamper-proof time the device maintains internally. But how to access this?

P.S. A discussion of this from 2010. The author of the penultimate comment tried this in a fallout shelter: so it's clear the phone is not getting the pristine time from any external source.

Addendum, July 2013

Found a few more posts (here, here and here) about another kind of time measure: system kernel boot time. It's accessed through a call something like this: sysctlbyname("kern.boottime", &boottime, &size, NULL, 0);. Unfortunately it too changes with the user-adjusted data and time, even without reboot. Another function gettimeofday() is similarly dependent on user-defined time.

2
  • 3
    I checked it out and found that gps.timestamp returns exactly the same time as [NSDate date].
    – kelin
    Aug 20, 2014 at 13:00
  • Indeed the timestamp properties returns the timestamp when this location was determined. Oct 11, 2018 at 15:36
4

NSDate and it's CF counterpart are all based on the user controllable time, and thereby aren't tamper proof.

As far as I know, there is no open API for either GPS time or carrier time directly. However, you can check the mach_absolute_time to get untampered time since last boot up, and perhaps use it to at least be aware of how much time has passed since the app has been awoken (without having the potential for that time to be tampered with while the app is running).

mach_absolute_time depends on the processor of the device. It returns ticks since the device was last rebooted (otherwise known as uptime). In order to get it in a human readable form, you have to modify it by the result from mach_timebase_info (a ratio), which will return billionth of seconds (or nanoseconds). To make this more usable I use a function like the one below:

#include <mach/mach_time.h>

int getUptimeInMilliseconds()
{
    static const int64_t kOneMillion = 1000 * 1000;
    static mach_timebase_info_data_t s_timebase_info;

    if (s_timebase_info.denom == 0) {
        (void) mach_timebase_info(&s_timebase_info);
    }

    // mach_absolute_time() returns billionth of seconds,
    // so divide by one million to get milliseconds
    return (int)((mach_absolute_time() * s_timebase_info.numer) / (kOneMillion * s_timebase_info.denom));
}
0

Even if you can get hold of the time from GPS you should be aware that GPS time is not quite the same as UTC. The GPS receiver in the iPhone might take care of that for you though.

1
  • Todays GPS chips delivers the time in UTC, too. So there are two interpretation of GPS time: UTC time from GPS, and real GPS time (currentlyabout UTC -14 seconds)
    – AlexWien
    Jul 10, 2013 at 0:05
-2

This gets you the current date and time:

NSDate *now = [NSDate date];

This will be as reliable as you can get. The internal clock on the iPhone will be updated when it can get access to an NTP server. If the phone uses GPS as a time sync source it'll also be used to update the same system-wide clock which is accessible via the above method.

The CoreFoundation equivalent is something like:

CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();

Which returns the CoreFoundation equivalent of the normal UNIX seconds-since-epoch timestamp.

1
  • Time returned by [NSDate date] can be changed by user.
    – kelin
    Aug 20, 2014 at 12:34
-2

The gold standard of timekeeping are the various government time observatories in the U.S. and worldwide. They provide Atomic time. That is used world wide. Apple should be using that. If the want to sync w/ the cell towers, there should be an Alternate internal time. If the tower or GPS system malfunctions all are left with incorrect time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.