From ab8dfd9f8ff3ccc5425a1c85289a1990c50bdd0c Mon Sep 17 00:00:00 2001 From: James Farrell Date: Wed, 13 Aug 2025 15:26:40 +0000 Subject: [PATCH] Switch Instant to use CLOCK_BOOTTIME on Android. This is a long-term patch we have carried for the Android platform: https://android.googlesource.com/toolchain/android_rust/+/refs/heads/main/patches/longterm/rustc-0018-Switch-Instant-to-use-CLOCK_BOOTTIME.patch --- library/std/src/sys/pal/unix/time.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/std/src/sys/pal/unix/time.rs b/library/std/src/sys/pal/unix/time.rs index 328fe0bc9603f..add7abedb3482 100644 --- a/library/std/src/sys/pal/unix/time.rs +++ b/library/std/src/sys/pal/unix/time.rs @@ -275,7 +275,9 @@ pub struct Instant { impl Instant { #[cfg(target_vendor = "apple")] pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_UPTIME_RAW; - #[cfg(not(target_vendor = "apple"))] + #[cfg(target_os = "android")] + pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_BOOTTIME; + #[cfg(not(any(target_vendor = "apple", target_os = "android")))] pub(crate) const CLOCK_ID: libc::clockid_t = libc::CLOCK_MONOTONIC; pub fn now() -> Instant { // https://www.manpagez.com/man/3/clock_gettime/ @@ -289,6 +291,9 @@ impl Instant { // // Instant on macos was historically implemented using mach_absolute_time; // we preserve this value domain out of an abundance of caution. + // + // On Android, we use CLOCK_BOOTTIME because we want the duration between + // Instants to not be affected by suspends. Instant { t: Timespec::now(Self::CLOCK_ID) } }