Cocoa system time?

Post: #1
Im trying to calculate frame rate in cocoa. Is there a good function for getting the system time in cocoa?

Im just learning the Cocoa API's. Sorry if this is a stupid question.. since im sure the answer is silly obvious.

Quote this message in a reply
Posts: 508
Joined: 2002.09
Post: #2
You'll have to use the following Carbon function: CFAbsoluteTimeGetCurrent() (which returns a value in seconds).

You could also use [NSDate date] but I don't think that's a good decision.

"When you dream, there are no rules..."
Quote this message in a reply
Posts: 5,143
Joined: 2002.04
Post: #3
+[NSDate timeIntervalSinceReferenceDate]

it returns a double, which is "seconds since 1970", but accurate to a few microseconds.
Quote this message in a reply
Posts: 1,403
Joined: 2005.07
Post: #4
I do it like this sometimes:

- (void)setupRenderTimer
NSTimeInterval timeInterval = 0.01f;
renderTimer = [ [ NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector( animate ) userInfo:nil repeats:YES ] retain ];

[ [ NSRunLoop currentRunLoop ] addTimer:renderTimer forMode:NSEventTrackingRunLoopMode ];
[ [ NSRunLoop currentRunLoop ] addTimer:renderTimer forMode:NSModalPanelRunLoopMode ];

worldTime = [NSDate timeIntervalSinceReferenceDate];

- (void)animate
animate([NSDate timeIntervalSinceReferenceDate]-worldTime);
worldTime = [NSDate timeIntervalSinceReferenceDate];

Sir, e^iπ + 1 = 0, hence God exists; reply!
Quote this message in a reply
Post: #5
Thanks everyone.

I implemented it like this:
I store CFAbsoluteTime oldTime (as global data).

-(void) initGL:
oldTime = CFAbsoluteTimeGetCurrent();

- (void) drawRect: (NSRect)rect:
CFAbsoluteTime newTime = CFAbsoluteTimeGetCurrent();
double frameRate = 1.0/((double)newTime - (double)oldTime);
oldTime = newTime;
// then I print out the frameRate value to the screen

does this make sense?

Of course this is using the carbon libraries in a cocoa app. Hopefully apple wont depreciate carbon anytime soon. =)
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Getting the system time? tobin 3 3,214 Feb 26, 2008 01:23 PM
Last Post: Frank C.
  Getting system time Fenris 2 3,178 Apr 25, 2005 07:31 PM
Last Post: Puzzler183