I just have a quick question about how date and time works in iOS and Objective-C. If I make a call like NSDate *today = [NSDate date] on two separate devices, will they both return the exact same time? I am trying to have multiple phones sync up using the current time and I just want to make sure that they will have the same current time (if it's a matter of milliseconds I'm not worried but more than a second off will probably not work).
Thanks in advance!
Since NSDate stores a time internally as an NSTimeInterval, which is a typedef for double. It has in the sub-microsecond resolution for times within a decade or more of the date of this question.
This means that even if your two devices have closely synchronized clocks, you are unlikely to get exactly the same NSDate by calling [NSDate date] on both devices.
You can check whether two dates are within a second of each other like this:
NSTimeInterval diff = date0.timeIntervalSinceReferenceDate - date1.timeIntervalSinceReferenceDate;
if (fabs(diff) < 1) {
// dates less than one second apart
}
Related
I use the current date (YYYY-MM-DD) in a critical part of my app, and I need the correct date independant of the set system date. So far, the only option that I found that WOULD work is using an NTP server like this one here.
Problem: This is complete overkill for my purposes as I can deal with a +- several hour inaccuracy of the date. I simply need a somewhat accurate datestamp at various points throughout the app. All of the devices using my app will be connected to the internet as well as GPS, etc. I would even be happy to find a simpler method of finding the current date that would only work on some select devices, just something. I am wondering if anybody knows of any other ways now that actual time accuracy is not so important.
EDIT: I need this date to be independent of device date because the user could easily change the device date rendering it inaccurate.
Thank you.
You should probably use NTP, or at least a subset of NTP since you don't need that much accuracy, perhaps simply accepting the first value returned from the NTP server.
As an alternative, if you run your own web server, you could create a very simple server-side application that simply returns the date via HTTP/HTTPS. To prevent users from spoofing the server, you'd probably want to use HTTPS rather than HTTP although really clever users could still get around that issue by installing private certificates on the device or a network proxy.
Note that depending on a server means that your application won't run if there's no network available.
An alternative would be to use the system date routines as others have described, but write the date to either NSUserDefaults or, better yet, the device keychain. That way, your application can tell if the user turned the date backwards. There may be some edge cases you'd have to watch out for, though, like time zone changes.
NSDateFormatter *dateFormat = [NSDateFormatter new];
[dateFormat setDateFormat:#"YYYY-MM-DD"];
NSString *dateAsString = [dateFormat stringFromDate:[NSDate date]];
I have this method in my Utilities class.
+ (NSString *)formattedDate {
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"yyyy-MM-dd"];
NSString *todaysDate = [dateFormatter stringFromDate:[NSDate date]];
return todaysDate;
}
I put it in the header for results like this:
NSString *gameNameHeader = [NSString stringWithFormat:#"<p> </p><h1>%# Results</h1><h2>%#%#</h2>",GAME_NAME_TITLE,[Utilities formattedClientName], [Utilities formattedDate]];
I am developing an app that has refills lives every 5 minutes. What is the best way to check the time since the last refill so that the user cannot trick the system by changing the time on their device?
For an example, look at Candy Crush. Changing the system time on there gives you extra lives, and this is what I need preventing.
How about running an [NSTimer scheduledTimerWithTimeInterval:300.0
target:self
selector:#selector(giveLife:)
userInfo:nil
repeats:YES];
That should execute giveLife every 5 minutes and there you can give the extra life.
As far as I can tell, NSTimer will not be affected by changing the clock time once it has started.
I would suggest You do something similar as it is in Clumsy ninja.
SHORT
Every time application becomes active [applicationDidBecomeActive:] - You try to get internet time.
If device time timestamp is smaller than stored timestamp - don't allow to continue. (Ask to connect to internet).
Implement a feature to save timestamp of the last regeneration cycle. Thus - even if user succeeds getting faster live refills, Once he reverts back to original time and continues game - it will show that next live refill will be way in the future at the time user was last using application, not 5 minutes. (To punish cheaters) (But maybe set a max limit, if last regenerate time is bigger than 5 minutes from current time, then set it to 1 hour? still a punish, but not so big to lose a player.
LONG
(Based on what I could distinguish)
Every time application becomes active - You try to get internet time. At that point - You don't need to check the time anymore while the app is opened, until the next time application becomes active again.
In clumsy ninja, they allow to use app even if internet is not available. So - if I activate airplane mode (for example), adjust time to be few hours forward, and open application - it sees that internet is not available, and thus cannot compare time. And - the things (punch bag, balls, etc) I had to wait to be repaired - are now repaired. Yay.
But If I do the same thing again many many times and finally I want to set back to original date and time - and open application, it will know that there is something not right. It probably stores some last opened timestamps, and if that time is bigger than actual device time - then user probably is doing something bad. And - alert appears - App must connect to server in order to continue.
Another downfall of moving time forward (for user, but great for developer) is - that there are some things, which will auto-regenerate after a while, but needs to be "picked" in order to initiate regenerating again. In clumsy ninja, chicken generates an egg once in a while. If Egg is not picked up, it still holds a timestamp, when it should be / was generated. So - imagine, user successfully forwards time many times (until it is already 1 day ahead of original time) - and now he wants to set the time back to original time, so that clock would actually show correct time. After user does that - when app is opened, time is synced but the generating egg will now show, that it will be ready after 1 day - last saved timestamp. But this could also be used for all items, which require time to be available. Why the clumsy ninja has not used this thing to all items? Maybe they are afraid, that if user sees, that nothing is available for a long time - user will stop playing. Thus - better have a cheater user, than no user at all.
NSURL *url = [NSURL URLWithString:#"http://www.timeapi.org/utc/now"];
NSString *str = [[NSString alloc] initWithContentsOfURL:url usedEncoding:Nil error:Nil];
str = [str stringByReplacingOccurrencesOfString:#"T" withString:#" "];
str = [str stringByReplacingOccurrencesOfString:#"+01:00" withString:#""];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"yyyy-MM-dd hh:mm:ss"];
NSDate *date = [dateFormat dateFromString:str];
I want to store times type value (but not the date) in Core Data. Which data type should I use?
Possible options:
Three integer attributes for hour, minutes, seconds.
One string attribute for "HH:MM:SS".
One integer attribute for the number of seconds since midnight.
The best representation depends on what type of queries involving the time you want to
execute. The first option is better if you want to search for a given hour, minute or second. The other options are better if you have to search for time intervals.
My approach would probably be to convert it to an HHMMSS format, either as an integer or a string. NSDate isn't appropriate because it is stored as a interval since an epoch (seconds since 1970)
The date portion of an NSDate is inherently part of time. NSDate is a NSTimeInterval number of seconds positive or negative since the reference date. With a locale and a timezone you can get an appropriate localized display string on the fly.
In most cases it makes far more sense to store the number behind the NSDate returned by timeIntervalSinceReferenceDate
It's small and portable and correct.
Then recreate an NSDate with dateWithTimeIntervalSinceReferenceDate:
Then use an NSDateFormatter with NSLocale and NSTimeZone to get an appropriate value for display to users.
It's an unwieldy and cumbersome set of tools that actually help you do it right.
The good news is all the effort you expend in doing this pays off in making it easier next time and giving you code that will do the right thing and have a backing value that is actually really easy to store and manipulate.
Additionally by using the API correctly you will lose no precision and have more possibilities open to you.
Trying to do it with format strings alone seems easy and simple but is the road to bad code.
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
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:
Apple's sparse documentation of CACurrentMediaTime
Stack Overflow - NSTimer vs CACurrentMediaTime()
http://bendodsonapps.com/weblog/2013/01/29/ca-current-media-time/
http://blog.spacemanlabs.com/2011/09/all-in-the-timing-keeping-track-of-time-passed-on-ios/
http://forum.sparrow-framework.org/topic/accurate-timer
Note: If you do use CACurrentMediaTime, make sure you include and link the QuartzCore.framework
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.
I've tried countless ways of getting the current timestamp and no matter what I do the timestamp is always off.
What is the proper way to get the right timestamp
NSLog(#"%#",NSTimeIntervalSince1970);
Was the closest one however, it's still not right. Any suggestions?
NSTimeIntervalSince1970 is a #define and gives you the number of seconds between 1970 & 1/1/2001. i.e. it'll always be the same. you want to do something like [[NSDate date] timeIntervalSince1970];