views:

341

answers:

1

I need to access the elapsed time since startup in nanoseconds from a Python program running on Mac OS X 10.6.

I use the following Carbon calls to get this in C code:

AbsoluteTime uptimeAbs = AbsoluteToNanoseconds(UpTime());
uint64_t elapsedTime = ((uint64_t)uptimeAbs.hi << 32) + uptimeAbs.lo;

Is it possible to get to these functions in Python using a Carbon or PyObjC module? I tried:

from Carbon import *
UpTime()

but got "NameError: name 'UpTime' is not defined".

How can I get access to this value from Python on OS X?

+2  A: 

Within the code in one of the answers at http://stackoverflow.com/questions/1597383/cgeventtimestamp-to-nsdate, I found -[NSProcessInfo systemUptime], available starting in 10.6. This gives me the time in decimal seconds, which I can multiply:

from Foundation import *
NSProcessInfo.processInfo().systemUptime() * 1e9

The result does have nanosecond precision, and should work nicely for my needs.

Update: the following method also works, and is compatible with 10.5 as well:

from Quartz.QuartzCore import *
CACurrentMediaTime() * 1e9
natevw