I'm trying to avoid creating an NSDate object since my code is heavy in needing the 'current time'. I end up calling [[NSDate date] timeIntervalSince1970] a lot. Is there any way to get the time interval without instantiating the object?
CFAbsoluteTimeGetCurrent gives you similar accuracy - no object required.
CFAbsoluteTimeGetCurrent uses a different base (or reference date), so you will have to initially determine the offset or the bases if you need to use another reference date (e.g. relative to some time in 1970).
NSTimeInterval interval = [NSDate timeIntervalSinceReferenceDate];
you deleted your comment regarding an option with more accuracy than CFAbsoluteTimeGetCurrent: try gettimeofday
Related
I know in python that you can get the time in milliseconds since 1972 or some time around there. I wanted to know if there was a similar feature in Objective-C or if I need to make something to calculate it.
Well, there's [[NSDate date] timeIntervalSince1970], which give the time (in seconds) since midnight on January 1, 1970.
[NSDate date] returns the current time; you can get the seconds-since-epoch relative to any time that you have an NSDate object for.
From the sound of it, you don’t care about the particular start date, just an elapsed-time number you can get milliseconds from. CFAbsoluteTimeGetCurrent() will give you a double-precision value for the current system time.
If it doesn't have to be Objective-C:
long timeSinceEpoch = time(NULL);
I'm creating a NSDate in the past (1 hour in the past) and that looks to be setting correct, only thing is I want to then use that to determine if an even has happened or not. Because I set to be in the past, when I do the check it should definitely think the even has happened, but timeIntervalSinceDate seems to only give a positive result?
This is the code I'm using with timeIntervalSinceDate
NSDate *now = [NSDate date];
NSTimeInterval secondsSincePlantingInterval = [now timeIntervalSinceDate:plantingDate];
Which is giving 68 seconds, but it should be -68 seconds, or does it not return negative values?
This is perfectly normal, documented behavior
The documentation states :
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate
Return Value
The interval between the receiver and anotherDate. If the receiver is earlier than anotherDate, the return value is negative.
Since plantingDate is in the past, the receiver is not earlier than it. Therefore the value is positive.
Moreover ; this is plain english
[now timeIntervalSinceDate:plantingDate];
So, the time interval since the plantingDate up to now is positive.
It does return negative values, yes. But if plantingDate is in the past then I'd expect secondsSincePlantingInterval to be positive.
If you think about it, that line is reading:
Tell me the number of seconds now is since plantingDate.
Just like Tuesday 2nd is one day since Monday 1st, now is +ve seconds since plantingDate.
You want:
NSTimeInterval secondsSincePlantingInterval = [plantingDate timeIntervalSinceNow];
I have a class which must have a property of some kind of timestamp, representing the moment of time when the instance of this class was created. And then when having multiple objects of this class I need to find the time interval between the creation of these objects.
And the usual interval is going to be up to 10 seconds, so I need precision of at least 1 second, but something like 0.1-0.001 second would be much better.
What is the best option to use for this property?
As far as I know, NSDate has precision up to 1 second.
I believe that I need something related to CFTimeInterval. I've used it for view animation with CADisplayLink. It provided the CFTimeInterval value for each moment of screen update and I could calculate the time interval between two CFTimeInterval's very easily.
But how do I assign the value to this CFTimeInterval at any moment of time?
NSDate will work, as Matthias Bauch's comment indicates. You can also just use CFTimeIntervals though:
CFTimeInterval currentTime = CFAbsoluteTimeGetCurrent();
Do note that this function and NSDate are both based on an absolute reference date (Jan 1, 2000 in this case). This means that if the system's clock is changed while your app is running, values obtained before the clock change won't correctly compare to values obtained afterward.
This is a completely noobish question, but I spent 2 hours yesterday trying to make it work, and I'm obviously missing something very basic.
What I need to do is take input from user of date/time and count back 90 minutes for an alert.
Could someone please post an example calculation, where you have a var that holds user input and a new var that receives the result of this computation? (all done in Objective C for use in an iPhone app) Thank you!
I suspect you could do something like:
NSDate *alertDate = [userDate dateByAddingTimeInterval:-5400.0];
I think this should work:
NSDate * alarmDate = [NSDate dateWithTimeInterval:5400 sinceDate:userDefinedDate];
NSDate * now = [NSDate date];
NSTimeInterval wait = [now timeIntervalSinceDate:alarmDate];
[self performSelector:#selector(callAlarm) withObject:nil afterDelay:fabs(wait)];
Although I do agree with Nick too, adding your work its much more productive..
Assuming you have a UIDatePicker, your target date will already be in an NSDate object. If it's coming from another source, you're probably ending up with it in an NSDate object, either from a string via an NSDateFormatter or by some other means.
From an NSDate object, you can get an NSTimeInterval relative to some absolute date. That's a C primitive type (it's a double in practice, but obviously don't code to depend on that) that you can do arithmetic directly on. So you can subtract 90 minutes directly from that. There are then various + dateWithTimeInterval... class methods on NSDate that will allow you to get a date from the result.
I need to find the specific time a tap happens and then the time since it has passed. I have the app counting taps, I just haven't figured out the time thing.
I tried:
timeStamp = [[NSDate date] timeIntervalSince1970];
but I'm new to obj c and clearly there is a syntax problem.
Thanks for anyhelp.
If you are trying to find the amount of time that has passed since an event, I would create an NSDate time stamp when that event occurrs:
NSDate *timestamp = [NSDate date];
Then, later on to check how long it has been since that timestamp you can call:
NSTimeInterval interval = [timestamp timeIntervalSinceNow];
NSTimeInterval is just a typedef. It is actually a double representing a number of seconds. In the above case interval will be the number of seconds since the timestamp. (Also note that it will be negative since your timestamp is in the past.)
The most obvious reason I see for a syntax error would be the declaration of timeStamp.
It should be:
NSTimeInterval timeStamp;