macOS System Uptime

am confused…
When I query system.microseconds I get an answer of


which is
20 days 19 hours 42 minutes

If I issue the terminal command UPTIME, I get
42 days 12 hours 35 min

if I ask Swift
I get 3,674,452 seconds or 42 days 12 hours 40 min (note they was a delay between each reading)

So my Swift solution matches the Termianl results, but is WAY WAY off from Xojo?

any idea why?

can also under several circumstances less than Terminal. Why: inconsistent timers. Means: not really but nearly really measuring the time

There is no solution like there would be none with Javascript. When the timeserver of an Application framework is different to the systems time framework it is not measuring really correct

Interesting that

let x = ProcessInfo.processInfo.systemUptime

return the same 20 days that Xojo does

and my last reboot was Jan 13… sigh which was 42 days ago!

meaning Xojo is WRONG as is the ProcessInfo method

no only the same calculation method but yeah: the Xojo method isn’t thread resistant. That makes it clear: it kills every precision.

btw. ProcessInfo is also not Thread safe and because of this we have this interesting reaction

public var microSeconds : Double {
        var temp = timespec()
        if 0 != clock_gettime(CLOCK_MONOTONIC_RAW, &temp) {
            fatalError("Could not execute clock_gettime, errno: \(errno)")

        let awake  = ProcessInfo.processInfo.systemUptime
        let upTime = Double(temp.tv_sec)

        print("awake =\(awake)  : \(awake/24/60/60) days")
        print("uptime=\(upTime) : \(upTime/24/60/60)")

        return Double(temp.tv_nsec)

note : AWAKE vs UPTIME
Xojo reports AWAKE

The Xojo docs say:
“Returns the number of microseconds (1,000,000th of a second) that have passed since the user’s device was started.”

So there is a Xojo bug. Either they are using the wrong Mac API, or the docs are wrong… More than likely it’s the former… But I can’t report it as Feedback is crashing on me.


Yeah… and they do a CYA in the next paragraph…

The machine's internal counters might or might not continue to advance while the machine is asleep,

which means it is NOT the time since device was started… :slight_smile:

in my terminal (Monterey / m1) is correct… (19 mn).

But it report 2 Users… while I am alone with this MacBook Pro.

probably root & you

On Mac, Unix, Linux use following command, to get the system running time:
ps -p 1 -o etime
On Mac, you can also use:
sysctl -a | grep kern.boottime
On Windows use:
wmic os get lastbootuptime

uh no…that provides a time based on 1970… and my computer has NOT been running for 52 years

kern.boottime: { sec = 1642140948, usec = 793095 } Thu Jan 13 22:15:48 2022

19006 days 6 hours 15 minutes 48 seconds

The data I posted above is in fact quite correct… it is just a matter of do you want “uptime” (time since last reboot) or as Xojo reports the “awake” time [time the computer has NOT been “asleep”, during the “uptime”)

This is for macOS… I’m not sure, but I think Windows reports “uptime” always

If you are interested not on the boot time but on wake time on Mac use:
sysctl -a | grep kern.waketime
and to retrieve the sleep time
sysctl -a | grep kern.sleeptime

If you read the code snippet I posted… it shows how to get either/both without the use of SHELL (which is not allowed by the AppStore)