iOS "real" time for specific TimeZone while offline - iphone

This is the scenario:
I'm writing a medical related program, that would be use while with no connection. When some action act, the program would write the time to CoreData record.
That's the problem, if their device set the time to a diff time like earlier than the real time. That would be a big problem coz it's for medical usage.
So, how can i get the "real" time even if there is no connection?
Or, is it possible to disallow user changing the device time using something like restrictions or DeviceProfile?

It's only because Apple IS the Big Brother they claimed to be fighting in 1984. Welcome to 1984! Otherwise we would have access to real time time, and an English version of ISO date format! :-/
Every iPwn, and now other devices, has a GPS receiver and an Internet capability, from which sub-second accurate time could be derived, yet Apple insists on forcing us to depend on AT&T to automatically set our clocks. It's only recently that AT&T started delivering accurate time, thank god for small favors.
The lack of GPS and NTP time setting, plus the glaring omission of ISO 8601 date formatting with otherwise USA formats and language, is extremely annoying on a daily basis.
So, the answer to your question is, Yes, it is feasible, but not in Apple's Jail, since you cannot set RTC from GPS or NTP without jail breaking.
PS: my guess is that AT&T insists on this for call-timing or something stupid that has to do with charging us more money! ;-)

You get the real time from [NSDate date]. For example, the following:
[[NSDate date] timeIntervalSince1970]
gives the number of seconds since 1/1/1970 midnight UTC. This is independent of timezone and independent of whatever time is set by the user. If you know the timezone, then you can convert that to local time with the NSDateFormatter if you like, but make sure to also record the timezone to make the representation unique.
EDIT: Sorry, this answer is actually not correct. After trying it out, it appears that setting the time by the user also changes the NSDate values.

So, how can i get the "real" time even if there is no connection?
If by "no connection" you mean "no network connection of any sort" the answer is that you can't.
I think the best you can do is disable the functionality if you can't find a way to independently verify the system time (and tell the user why).

NSDate and all associated classes read the date from the system time. Without an internet connection to refer to, the NSDate class is open to user abuse.
I used an example in a comment on fishinear's answer of Farmville. If the user plants some crops that take five hours to grow, you can just change the system time to five hours in the future to harvest. Which, I'm sure, is one of the reasons that Zynga requires an internet connection to play their games.
Without some time-telling hardware, Apple cannot realistically tell time without an internet connection; even if they did have some amazing solution, they'd have to take into account timezones and even travel across timezones in order to make this work.
If I were you, I'd require an internet connection at specified intervals (once a day or the like) in order to draw a reference.
Let us know why you need this and we may be able to suggest some viable alternatives in function.

Related

Can I to know if ipad was rebooted?

I am developing an application that must work online and also offline. This application should sync informations with our server. For this, we need that the device utilize the server clock.
I found a lot of information, and I get the following idea:
When the user logins online I will force him to get the server clock time. In this moment he obrigatory must have internet connection, so it is ok.
When I get the clock server time, I get the systemUptime information that says the interval that the device is turned on, and I store it. I can get systemUptime like this:
[NSProcessInfo processInfo].systemUptime
When the user to create a new local file, I will know the current interval based on systemUptime function, so I know the current time, and I don't depend the iOS system clock.
The problem is: Everytime that device is rebooted, or turned off, the systemUptime is reseted. Until here OK, I can solve it forcing the user to login again, and getting the server clock time again. My problem is to know when the device was rebooted. Can you help me? Thank you guys!
My advice would be to not refer to the device time at all. Get the the files from the server and have the server also answer the server time that the files are retrieved. You can store this in the file, in the file name, or separately on the device.
At some point in the future ask the server if there's a file newer than the server time you recorded earlier.
In this sense, the time isn't really a time at all, it's a version number, and you could make that explicit with the server too, using just an integer from the server that indicates progressing sequence of versions.
If it's important that your app be strong in this way, your only choice is to remain independent of device time. Otherwise, there are too many ways it can break (including small time errors due to latency on the time check, or device factory resets or malicious actions by a user). It's better to remain independent of device time if you can.
When you get the systemUptime the first time, subtract it from the current (iOS) date and time. That's the time the system was last brought up.
Then recalculate this value whenever necessary. If the "time the system was last brought up" has changed, then you know it's time to log in again.
However, as suggested by another commenter, I suspect there's a better way to tackle this from scratch.

Is there any way to get the tamper-proof date and time on iPhone?

For various reasons I need to get from the iPhone the current date and time that can't be meddled with by the user. Yes, I've seen how one can check a server (e.g., here), but that's not invulnerable to tampering if you take a moment to reflect.
There are two knee-jerk reactions I'm expecting to hear:
Use the GPS time.
It can't be done.
In answer to another question, I've described my researches into this matter. To summarize them:
The GPS time shifts with the user-defined settings.
The iPhone definitely has an internal tamper-proof time and date, as shown when date-time reverts after Set Automatically in Settings > General > Time & Date is turned back to on even in a fallout shelter.
What I want to know is how to access this tamper-proof time.
Edit
Just to be clear, the server-based solution is not suitable. For one, it could be faked. For another, the app needs to work without a network connection.
Assuming you always have Internet available, you could implement a class or object that connects to a remote Network Time Protocol server.
Here's an open source GitHub project that should get you started, and the related StackOverflow question I found it at.

Timezone issue with webserver and iPhone app

I'm having issues with my webserver and iPhone app, considering the timezone. It's all set well and I know how to change it, but I'm just wondering what is the best approach considering the following case.
The iPhone app I am working on uses several NSDate objects to store datetime values. These get exchanged with the webserver (including database) that uses timestamps for comparing dates. For exporting the NSDate object I usually convert it to a string by formatting NSDate timeIntervalSince1970. I don't know what the exact timezone of this interval is, but I'm guessing it's the locale (if not set otherwise).
My webserver is set to CET (UTC-1). When the iPhone app converts the interval to, say, UTC-3, and the database compares it with a UTC-1 date, you obviously get a gap.
What's the best approach here? How does timeIntervalSince1970 handle timezones? I'm well aware that NSDate has no timezone (a timezone is merely representation), but since I'm converting from and to unix time through a string, I think it matters.
Any help is greatly appreciated.
Kind regards,
Reinder
Just always use UTC time. Both on client and server.
Yes, I would agree with the previous post to keep the server and iPhone database on the same timezone (UTC). I would only mess with the different timezone when actually displaying something on screen such as in a UITextView, when you would use the local time. So if someone in Sydney sync'ed with the server at the same time as someone in London, the server and both their iPhone's database would have the same datetime stored, but would display different times.

Unix timestamp question, different times

I am working with Unix timestamps, but I get a different time on my Android phone and my computer than the time given on http://www.unixtimestamp.com/index.php
The time difference is one hour (give or take a few seconds probably, didn't ever design an extremely accurate test).
I thought that the Unix time was the same everywhere on earth (or perhaps the universe, but that brings up questions of frames of reference and velocities that I don't even want to think about).
Basically my question is, what is going on here? I get the time on the Android device using Date date = new Date(); long now = date.getTime(); (I account for this having milliseconds btw) and I connect through putty to a linux server and the time is the same (obtained by date +%s). It's just the website that disagrees on the time, and since I don't know too too much about Unix timestamps and the site looks legit I wonder how this happened.
ps. I live in Saskatchewan, which doesn't observe daylight savings, but I don't see how that would really factor in. Does the website read the time from my computer maybe?
pps. sorry for rambling
That web site displays the wrong time.

How to check the time and date online?

hi i am making an app in which user sends xml to server and get response....
now i have to save the date and time of sending and response of that request....for this should i use iphone's date or check it online?
because my thinking is that the iphone's date and time may not be correct.....always...so it is better to check the time online ....
i know the local part but need help regarding online check....please help
Just use the local time. It will save you a lot of trouble and work. Or is your app so absolutely dependent on the correct time, like to display the current position of the sun or something ? If not, the local time is likely "good enough".
If you need the same time as the server, then it would seem that you need to establish the offset between server time and local time, and then you can adjust your local timestamp.
Is there a way to get the (current) time from the server?
Assuming the timestamp you're saving is for the request sent from the iphone and the response received by the iphone, then it would actually seem logical to use local (iphone) time.
You could always use NTP (http://en.wikipedia.org/wiki/Network_Time_Protocol) to get the current time, but that doesn't guarantee the time is thesame as the server.
You usually don't need absolute time of some sort, just the same time source as your server and your server's database.
So compare your local time with whatever the server thinks the time is and adjust accordingly.