timeIntervalSinceDate in past? - objective-c

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];

Related

How To Get Time Since Like 1972 in Objective-C

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);

How do I count time in iOS?

I don't want to set up a timer that "fires" (and does something) after a certain amount of time has passed.
Therefore, I'm not interested in the NSTimer class and its methods.
All I'm interested in is counting the time that passes by while, for example, some while-loop is executing. I could use NSDate as follows I guess :
NSDate currentDate = [[NSDate alloc] init];
while(someConditionIsTrue)
{
// executing..
}
timeElapsed = [self timeIntervalSinceDate:currentDate];
NSLog(#"time elapsed was: %i", timeElapsed);
However, if I understand correctly, timeIntervalSinceDate: can only count seconds.
Is there a way I can count the time that is passing by in milliseconds?
In other words, what is the smallest unit I can count passing time in and how ?
Look at CFAbsoluteTimeGetCurrent()
CFAbsoluteTime before = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime after = CFAbsoluteTimeGetCurrent();
Your second approach is correct. Save the current date in an NSDate object and use timeIntervalSinceDate: to get the passed time since then. The result will be of type NSTimeInterval which is a floating point number. It specifies time differences in seconds, but since it's a floating point number it can store fractions of a second as well.
NSTimeInterval is always specified in seconds; it yields
sub-millisecond precision over a range of 10,000 years.

Calculating time 90 minutes prior to due date/time in Objective C (iPhone app)

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.

timestamp and subtract objective C

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;

Converting JSON date(ticks) to NSDate

Does anyone know how to convert a JSON date(ticks) to an NSDate in Objective-C? Can someone post some code?
I'm guessing here but your JSON value is the number of milliseconds since 1970, right? You can use NSDate's dateWithTimeIntervalSince1970: method to return an NSDate object with the correct time. Just make sure to convert the JSON milliseconds number to seconds before passing it to NSDate-- Cocoa uses NSTimeInterval in most places, which represents an interval in seconds.
It goes roughly like this:
// Input string is something like: "/Date(1292851800000+0100)/" where
// 1292851800000 is milliseconds since 1970 and +0100 is the timezone
NSString *inputString = [item objectForKey:#"DateTimeSession"];
// This will tell number of seconds to add according to your default timezone
// Note: if you don't care about timezone changes, just delete/comment it out
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
// A range of NSMakeRange(6, 10) will generate "1292851800" from "/Date(1292851800000+0100)/"
// as in example above. We crop additional three zeros, because "dateWithTimeIntervalSince1970:"
// wants seconds, not milliseconds; since 1 second is equal to 1000 milliseconds, this will work.
// Note: if you don't care about timezone changes, just chop out "dateByAddingTimeInterval:offset" part
NSDate *date = [[NSDate dateWithTimeIntervalSince1970:
[[inputString substringWithRange:NSMakeRange(6, 10)] intValue]]
dateByAddingTimeInterval:offset];
(from https://gist.github.com/726910)
You'd have to detect the client's locale in order to be able to do that, and unless your client knows how to do that, there's probably not much point.
NSDate's descriptionWithLocale: would be the way you format it for another locale. And timeIntervalSince1970 will go back to the (seconds) since 1970, which you could multiply by 1000 to get ms to return to the client. It's all in the NSDate documentation.
http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSDate_Class/Reference/Reference.html
According to this page: http://msdn.microsoft.com/en-us/library/system.datetime.ticks.aspx ticks begin on Jan 1, 0001 so dateWithTimeIntervalSince1970: is not automatically setup to work with ticks. You can still use this method but should adjust for the difference.