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;
Related
I am trying to make a timer and yes I have looked at NSTimer and it is not what I'm looking for, I basically need a way to convert a NSDate in the format of ("hh:mm:ss") into the amount of seconds that would be in this. And then I need a way to do the opposite so convert the seconds to the amount of hours, minutes and leftover seconds from that.
You can extract components from NSDate using the NSCalendar class. The following example extracts a single component, NSCalendarUnitSecond, from the current date.
NSDate *today = [NSDate date];
NSCalendar *localCalendar = [NSCalendar currentCalendar];
NSUInteger seconds = [localCalendar component:NSCalendarUnitSecond fromDate:today];
You could have a place holder date e.g. 00:00:00 and then use the NSDate function timeIntervalSinceDate: to get the seconds. Converting back again you could use the dateWithTimeInterval: method.
Obviously both these dates would have to be the same year month and day and you could use any arbitrary values for these as long as they are the same.
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 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.
I'd like to get the current hour and minute as integers. So if right now is 3:16am, I'd like to get the two integers: 3 and 16.
But it looks like [NSDate date] will give the number of seconds since 1970, or it can give a string of the current time representation, but there is no easy way to get them as integers?
I see a post in Getting current time, but it involved NSDateComponents and NSCalendar? That's way too complicated... all that was need is something like
NSDate *date = [NSDate date];
int hour = [date getHour]; // which is not possible
Is there a simpler way than using 3 classes NSDate, NSDateComponents, and NSCalendar to get the current hour as an integer, or typically, in Objective-C, would we typically still use C language's localtime and tm to get the hour as an integer?
How you interpret the seconds since 1970 depends on the calendar that you are using. There is simply no other option. Fortunately it is not that difficult to set up. See the 'Data and Time Programming Guide' for lots of examples. In your case:
// Assume you have a 'date'
NSCalendar *gregorianCal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *dateComps = [gregorianCal components: (NSHourCalendarUnit | NSMinuteCalendarUnit)
fromDate: date];
// Then use it
[dateComps minute];
[dateComps hour];
So it really isn't that complicated.
Also note that you could create a 'Class Category' to encapsulate this as:
#interface NSDate (MyGregorianDateComponents)
- (NSInteger) getGregorianHour;
- (NSInteger) getGregorianMinute;
#end
NSDate just holds the time that has passed since a certain reference date, to get more meaningful numbers out of this (eg. after taking care of DST, leap years and all the other stupid time stuff), you have to use NSDateComponents with the appropriate NSCalendar.
My class can help.
https://github.com/TjeerdVurig/Vurig-Calendar/blob/master/Vurig%20Calendar/NSDate%2Bconvenience.m
I'm sure you can figure out the minute part :)
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.