systemUptime not adequate to get the elapsed realtime since boot

From the documentation:
Compiler systemUptime
The amount of time the system has been awake since the last time it was restarted.

I have made some test on IPhone and I assume that the time doesn't contain the duration the phone is in deep sleep.
How I can obtain the elapsed time since the system was booted (including any time the device was in a low-power state).
My app cannot run continuously and needs to track elapsed time since the system was booted, including periods when the app is not running (e.g., during deep sleep), I'll need to rely on system APIs that provide this information accurately across app launches and system sleep states.

Here‘s your answer: System Uptime | Apple Developer Forums

1 Like

The date mentioned in the forum represents the boot time relative to the current system clock. This can be manipulated by the user, especially when the device is offline. While it's possible to verify if the wall clock is set correctly via NTP when online, this doesn't solve the problem entirely.

I'm working on elaborating on the use of this function.
let elapsed_time_nanos = clock_gettime_nsec_np(CLOCK_MONOTONIC_RAW)
mentioned here: https://developer.apple.com/documentation/kernel/1646199-mach_continuous_time

Have you actually tested this or are you just making the assumption? The manpage for clock_gettime says:

CLOCK_MONOTONIC    clock that increments monotonically, tracking the time
                   since an arbitrary point, and will continue to increment
                   while the system is asleep.

increments monotonically means never decreases, which implies the time may stay constant. :thinking:

Linux is one OS whose CLOCK_MONOTONIC does not increment during suspend. This behavior is allowed by POSIX, but it’s obviously much less useful, so Linux 2.6.39 added CLOCK_BOOTTIME.