0

I have this method below that checks for the time and it currently grabs what the iOS user has for date and time in iOS settings. However, it is possible for my App to not work as intended if they set a bogus date. I am wondering if it is currently possible to get the real GMT time and not rely on iOS settings or an online service.

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setAMSymbol:@"AM"];
    [formatter setPMSymbol:@"PM"];
    [formatter setDateFormat:@"MM/dd/yyyy hh:mm:a"];       

     NSString *dateString = [formatter stringFromDate:[NSDate date]];

     NSLog(@"THE DATE STRING%@",dateString);
user3606054
  • 591
  • 1
  • 5
  • 14
  • 3
    Where do you imagine the time would come from? – Alex Brown Jan 16 '15 at 21:47
  • Yes, I figured at much. I was hoping iOS had some super duper secret internal clock. – user3606054 Jan 16 '15 at 21:53
  • 2
    The only thing I would suggest if that your app can know if time goes backwards by saving the time each time it's launched (or whenever) and looking suspiciously upon backward changes. – Alex Brown Jan 16 '15 at 21:58
  • I think there are no ways to do that. see http://stackoverflow.com/q/1444456/629118 – Kazuki Sakamoto Jan 16 '15 at 21:59
  • From other SO I note that GPS is suggested as a time source:see http://stackoverflow.com/questions/14559583/getting-gps-satellites-and-atomic-clock-time-stamp-in-ios . Do you consider that online? It's certainly magical. – Alex Brown Jan 16 '15 at 22:02
  • Not really... Check [kelin](http://stackoverflow.com/users/3429577/kelin)'s comment: "I checked it out and found that gps.timestamp returns exactly the same time as `[NSDate date]`." – fpg1503 Jan 17 '15 at 01:41

2 Answers2

1

If the device is offline and the date is wrong there is no way to know it.

If you manage to get it, though, you can format it to GMT adding [formatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]] to your current code.

fpg1503
  • 7,492
  • 6
  • 29
  • 49
  • I beg your pardon > "I am wondering if it is currently possible to get the real GMT time and not rely on iOS settings or an online service." – fpg1503 Jan 16 '15 at 21:51
  • Right. The question is asking about some unknown source for the time - other than the device settings or the Internet. The question doesn't really have anything at all to do with formatting. – rmaddy Jan 16 '15 at 21:52
  • 1
    Not only does they want unknown source of time, they want it to be GMT. Regarding GMT you do it like I said, regarding the unknown source I said it's impossible. Don't get me wrong but I fail to see how my answer does not fulfil the question on its whole. – fpg1503 Jan 16 '15 at 21:56
  • 1
    My original comment was made before you added the info beyond setting the timezone on the date formatter. – rmaddy Jan 16 '15 at 22:12
  • I had accidentally hit enter, I edited it a couple of seconds later :) – fpg1503 Jan 16 '15 at 22:15
  • 1
    Or you can use `[NSTimeZone timeZoneForSecondsFromGMT:0]`, too. – Rob Jan 17 '15 at 01:06
0

There are internet time servers that let you make a API call to get the current time. If you have an internet connection you could use one of those. I don't know specifics though, just that they exist.

EDIT: I missed the part of your question where you said "...and not rely on... an online service."

Given those restrictions, the answer is no. If the user changes the local time on the device you MUST connect to a remote source of time information. There's no magic here.

Duncan C
  • 128,072
  • 22
  • 173
  • 272