System Uptime

Hello! I have a question. Is there a way to get the last time that the phone was booted? I found this:


ProcessInfo.processInfo.systemUptime


But it give me the time since the last time the phone was turned off, and I need to know the time since the last time the phone was switched on. I don't know if this is possible.

Accepted Answer

systemUptime
is not the droid you’re looking for. However, the problem is not what you think it is. You wrote:

But it give me the time since the last time the phone was turned off …

which is incorrect. Rather, the problem is sleep.

systemUptime
only counts time while the system has been awake, and our devices sleep a lot.

You can get the boot time (that is, the clock time when the system booted) using the

kern.boottime
sysctl. Here’s a snippet:
func bootTime() -> Date? {
    var tv = timeval()
    var tvSize = MemoryLayout<timeval>.size
    let err = sysctlbyname("kern.boottime", &tv, &tvSize, nil, 0);
    guard err == 0, tvSize == MemoryLayout<timeval>.size else {
        return nil
    }
    return Date(timeIntervalSince1970: Double(tv.tv_sec) + Double(tv.tv_usec) / 1_000_000.0)
}

Be aware that this time will change if the system clock changes, that is, the value is the boot time relative to the current system clock.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Answers

That's a bit surprising, as doc states :


var systemUptime: TimeInterval

The amount of time the system has been awake since the last time it was restarted.

Yes, I read that, but I think is a little bit ambiguous. So I already tried and the behaviour that I described above is what I got. So I'm looking for other options.


Thank you for reading.

If systemUptime does not show the amount of time the system has been awake since the last time it was restarted,

you should file a bug report.

systemUptime
is not the droid you’re looking for. However, the problem is not what you think it is. You wrote:

But it give me the time since the last time the phone was turned off …

which is incorrect. Rather, the problem is sleep.

systemUptime
only counts time while the system has been awake, and our devices sleep a lot.

You can get the boot time (that is, the clock time when the system booted) using the

kern.boottime
sysctl. Here’s a snippet:
func bootTime() -> Date? {
    var tv = timeval()
    var tvSize = MemoryLayout<timeval>.size
    let err = sysctlbyname("kern.boottime", &tv, &tvSize, nil, 0);
    guard err == 0, tvSize == MemoryLayout<timeval>.size else {
        return nil
    }
    return Date(timeIntervalSince1970: Double(tv.tv_sec) + Double(tv.tv_usec) / 1_000_000.0)
}

Be aware that this time will change if the system clock changes, that is, the value is the boot time relative to the current system clock.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Eskimo! Thank you very much! This is the third time you save me.

@eskimo, we are using this logic to identify device reboot, But without even device restart getting different value for tv. Getting difference up to 30 seconds. What may be possible root cause of this issue? any pointers.