Skip to content

Implementation of StdClock is not monotonic. #4330

Closed

Description

@jedel1043

According to JsInstant's documentation:

This type is guaranteed to be monotonic, i.e. if two instants are compared, the later one will always be greater than the earlier one. It is also always guaranteed to be greater than
or equal to the Unix epoch.

However, StdClock's implementation uses SystemTime, which on its documentation specifies:

Distinct from the Instant type, this time measurement is not monotonic. This means that you can save a file to the file system, then save another file to the file system, and the second file has a SystemTime measurement earlier than the first. In other words, an operation that happens after another operation in real time may have an earlier SystemTime!

This makes it possible to break the invariants of JsInstant.

Activity

  1. added
    A-BugSomething isn't working
    A-APIChanges related to public APIs
    on Jul 11, 2025
  2. abhinavs1920 commented on Feb 21, 2026

    @abhinavs1920
    Contributor

    Heyy @jedel1043
    Good catch, you’re right about the mismatch here. JsInstant promises monotonicity, but StdClock currently uses SystemTime, which doesn’t guarantee that.

    I’m planning to look into this and see what the best fix would be, likely involving a monotonic time source. I’ll follow up with a PR once I’ve had a chance to investigate.

    Thanks for flagging this.

  3. abhinavs1920 commented on Feb 21, 2026

    @abhinavs1920
    Contributor

    While working on this (PR #4652 ), I ran into a failing test locally:

    • builtins::date::tests::date_proto_set_utc_hours

    The failure is a mismatch between two numeric timestamps:

    • left: 1594208775779.0
    • right: 1594206975779.0

    The difference is exactly 30 minutes (1800000 ms), which suggests a local time‑zone / offset issue.

    As far as I can tell, this test uses helpers like timestamp_from_utc and from_local from the time crate to compute expected values. Those helpers do not go through the engine Clock / StdClock, so this seems independent of the monotonic StdClock change.

  4. abhinavs1920 commented on Mar 3, 2026

    @abhinavs1920
    Contributor

    Heyy @jedel1043 ..
    I have a clarification question: Would you prefer JsInstant to be purely monotonic and independent from system time?

  5. jedel1043 commented on Mar 3, 2026

    @jedel1043
    MemberAuthor

    Yes, to mirror what Instant is

  6. abhinavs1920 commented on Mar 3, 2026

    @abhinavs1920
    Contributor

    Got it, that makes sense. I’ll update the implementation to mirror Instant semantics and remove any system-time dependency.

  7. added a commit that references this issue on Mar 15, 2026
    ea849b7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-APIChanges related to public APIsA-BugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions