Objective C: Compare timeInMillis with current time - iphone

In my iPhone application, I need to calculate the time difference between the time a message was created on the server, and the time my phone received it.
The server (Java) puts in a number returned by System.currentTimeMillis() as metadata along with the message.
How do I compare this number with the current time on the device? Could not find a suitable NSDate method to do this comparison.
Thanks in advance!

You might take a look at this SO answer and the -timeIntervalSinceDate: method.

You can use (NUInteger) ([[NSDate date] timeIntervalSince1970] * 1000)

Related

How to print Time stamps through out the program in iOS

Some of the operations in my app are taking a lot of time to execute. SO, i am planing to print time stamps from starting of the app execution to end in order to check the time taken for certain operations. Is there any way in order to do that so that i can keep track of all the time stamps in a continuos way...
You can just NSLog(#"Any sensible log message describing your location in the code") - the output in XCode contains the time it was logged, to the millisecond.
You can define a var at the start of the methods you want to analyse:
NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
and then print it at the end:
NSLog(#"It took %f seconds in method <methodname>",[NSDate timeIntervalSinceReferenceDate] - start);
good luck!

how can I store different times from stopwatch

I am confused regarding to calculation of two different times. In my game, when game starts then timer gets started (like stop watch) and it stops when game gets finished. Now I have to store best low time among previous time list.
I am getting time in hh:mm:ss format. how can I store this time so that i can compare it with different time in list ? I tried to store this value in NSString, but the comparison fails.
EDITED :
Let me clarify the Question :
For example how can I store different times from stopwatch and how to sort it in ascending order ?
any suggestions?
Thanks...
Take two NSDates, one at the game start and one at game finish, then calculate the difference.
NSDate *startDate = [NSDate date]; // At game start
NSTimeInterval interval = [[NSDate date] timeIntervalSinceDate:startDate]; // At game finish
NSlog(#"interval: %.2f", interval);
You could use
long stamp = (long)[[NSDate date] timeIntervalSince1970];
for each of your time-relevant situation since the posted code is giving you a UNIX-timestamp. This timestamp can be used in arithmetic operations/comparisons which should be exact what you are looking for.

CSLogItem timestamp format (NSTimeInterval since when?)

I'm logging CMAccelerometerData on the iphone. CMAcceleration is a subclass of CMLogItem, which defines a timestamp. The timestamp is very important to me, as I have to match CMAcceleration data to positions obtain via GPS. GPS gives me a NSDate object, which is good; but CMLogItem gives me a number that I cant relate to anything.
It's not UNIX time for sure. This morning (17/11/2010 at 11H17am, in Australia), it was at 4090 seconds.
Any ideas of what that could be?
Nevermind I found out, CMLogItem timestamp is a NSTimeInterval starting when the phone last boot up.

How can I create a count up timer with the format seconds:milliseconds?

I'm diving into iOS development and I'm trying to create a count up timer in one of my views. I have the NSTimer code figured out to call a selector once every 0.04 seconds that updates the UILabel. Where I'm having trouble is with the formatting of the current time (starting initially at 00:00). I figured the best way to do this was using the NSDate class, and related classes (NSDateFormatter, NSDateComponents, etc.), but the manipulating of the dates and formats is really confusing me and the code is getting unwieldy quickly. I was hoping there are some SO users that are comfortable using the NSDate class that could help me figure out a simple way to calculate the current time for a count up timer and convert it to an NString with the format 'seconds:milliseconds'.
I'd be happy to post my initial attempt at the NSDate code if requested, but I won't initially because it's really of no use and embarrassing :)
If you just want to display time elapsed since you started your timer you can store starting date somewhere (say, startDate variable) and calculate time interval using current date. Something like the following should work:
NSTimeInterval passed = [[NSDate date] timeIntervalSinceDate: startDate];
double intPart;
double fract = modf(passed, &intPart);
label.text = [NSString stringWithFormat:#"%d:%.2f", (int)intPart, fract];

Is there an objective-c/iPhone version of currentTimeMillis() from Java?

I need to time some events in the app I'm working on. In Java i used to be able to call currentTimeMillis() but there doesnt seem to be a version in Objective-c. Is there a way to check there current time without creating a NSDate and then parsing this object everytime i need this information?
Thanks
-Code
[[NSDate date] timeIntervalSince1970] * 1000 returns a the same value as currentTimeMillis()
CFAbsoluteTimeGetCurrent() and [NSDate timeIntervalSinceReferenceDate] both return a double starting at Jan 1 2001 00:00:00 GMT.
There's also CFAbsoluteTimeGetCurrent(), which will give you the time in double-precision seconds.
To get very cheap very precise time, you can use gettimeofday(), which is a C Function of the BSD kernel. Please read the man page for full details, but here's an simple example:
struct timeval t;
gettimeofday(&t, NULL);
long msec = t.tv_sec * 1000 + t.tv_usec / 1000;
Instead gettimeofday() you could also use [NSDate timeIntervalSinceReferenceDate] (the class method) and do your calculations with that. But they have the same problem: they operate on "wall clock time". That means your measurement can be off if leap seconds are added while your test is running or at the transition between daylight saving time.
You can use the Mach system call mach_absolute_time() on OS X and iOS. With the information returned by mach_timebase_info() this can be converted to nanoseconds.
The correct answer is [[NSDate date] timeIntervalSince1970]; this will give you current timestamp in milliseconds.
The answer given by #Noah Witherspoon returns current date but the year is not the current matching year.