Skip to content

Clock.getDefault() isn’t configurable; add platform-aware selection & override hooks #1364

@Sabaev

Description

@Sabaev

Component(s): sdk, common, bootstrap
Environment: Cross-platform (JVM & Android)

Summary

Clock.getDefault() effectively hardwires System.nanoTime(). On Android this ignores deep sleep, leading to incorrect elapsed-time semantics for features like timeouts and background durations. There’s no easy global way to swap the default clock per platform.

Expected behavior

The SDK should choose a platform-appropriate default clock and allow an explicit override:

  • JVM/server: System.nanoTime() is fine for monotonic elapsed durations.
  • Android: prefer SystemClock.elapsedRealtimeNanos() (includes deep sleep).

Actual behavior

Users cannot globally configure the default clock. Each component must be hand-wired (if possible), which is fragile and easy to miss.

Proposed change

Introduce a lightweight, platform-aware ClockProvider SPI with discovery (optimized for Android, similar in spirit to kotlinx.coroutines’ FastServiceLoader):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions