There is a function UpTime declared in DriverServices.h. I believe this is equivalent to another function mach_absolute_time. Both seem to be undocumented.
Unfortunately the "sysctl kern.boottime" returns the seconds of the timestamp, not elapsed seconds.. Multiple calls do not increase the second count, but must be seconds from epoc of the boot date itself.
There is also a method of using sysctl
to call the system's last boot
time: $ sysctl kern.boottime
kern.boottime: { sec = 1271934886,
usec = 667779 } Thu Apr 22 12:14:46
2010
A simple Lua script to do exactly what you ask for:
local now=tonumber(io.popen("date +%s"):read())
local boottime=tonumber(io.popen("sysctl -n kern.boottime"):read():match("sec = (%d+)"))
local uptime=now-boottime
Old question, I know, but I needed to do the same thing so I thought I'd post the code I'm using, which I got from http://cocoadev.com/wiki/FindingUptime
There is a function
UpTime
declared in DriverServices.h. I believe this is equivalent to another functionmach_absolute_time
. Both seem to be undocumented.Unfortunately the "sysctl kern.boottime" returns the seconds of the timestamp, not elapsed seconds.. Multiple calls do not increase the second count, but must be seconds from epoc of the boot date itself.
The Uptime article on Wikipedia has an interesting lead:
Which references sysctl(8), which references sysctl(3).
A simple Lua script to do exactly what you ask for:
Old question, I know, but I needed to do the same thing so I thought I'd post the code I'm using, which I got from http://cocoadev.com/wiki/FindingUptime
correct way:
also you could use
CACurrentMediaTime()
from QuartzCore.framework (which has same code probably) - Available since OS X v10.5 and iOS 2.0