I think that across operating systems, the clock ids mean different things. So that values returned by iOS for elapsedRealtime and uptime are reversed, than what they are on Android. Citing from the sources I could find...
elapsedRealtime
As per the code, elapsedRealtime()
will fetch value from either of the following clocks:
Linux/Android: CLOCK_BOOTTIME
(from https://linux.die.net/man/2/clock_gettime)
Identical to CLOCK_MONOTONIC, except it also includes any time that the system is suspended. This allows applications to get a suspend-aware monotonic clock without having to deal with the complications of CLOCK_REALTIME, which may have discontinuities if the time is changed using settimeofday(2).
This is correct, Android works fine, and elapsedRealtime()
seems to return a time that is greater than uptime()
.
iOS: CLOCK_UPTIME_RAW
(from http://www.manpagez.com/man/3/clock_gettime/)
clock that increments monotonically, in the same manner as CLOCK_MONOTONIC_RAW, but that does not increment while the system is asleep. The returned value is identical to the result of mach_absolute_time() after the appropriate mach_timebase conversion is applied.
This is opposite to what is expected from elapsedRealtime()
uptime
Now if we look at the call for uptime()
Linux/Android: CLOCK_MONOTONIC
(from https://man7.org/linux/man-pages/man2/clock_gettime.2.html)
The CLOCK_MONOTONIC clock is not affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP. This clock does not count time that the system is suspended.
iOS: CLOCK_MONOTONIC
(from https://opensource.apple.com/source/Libc/Libc-1158.1.2/gen/clock_gettime.3.auto.html)
(as well as from http://www.manpagez.com/man/3/clock_gettime/)
clock that increments monotonically, tracking the time since an arbitrary point, and will continue to increment while the system is asleep.
It seems to me that the behavior for Apple/iOS is reverse of what it is on Android. I can also see that elapsedRealtime()
seems to return a time that is lesser than uptime()
in iOS, which is the reverse of what I see on Android.
Look forward to your advice on this. Am I reading the wrong sources? Something wrong with my understanding? Or is it just the exact opposite behavior on two OS's.