classifyClockSample function

ClockJudgement classifyClockSample({
  1. required ClockSample previous,
  2. required ClockSample current,
  3. Duration normalTolerance = const Duration(seconds: 5),
  4. Duration suspiciousJumpThreshold = const Duration(hours: 1),
})

Pure classifier — no storage, no singletons — comparing current against the previous recorded sample. Exposed directly (not just through TrustedClockService) so a test can drive the full normal/rewind/jump/reboot matrix without touching StorageService.

Implementation

ClockJudgement classifyClockSample({
  required ClockSample previous,
  required ClockSample current,
  Duration normalTolerance = const Duration(seconds: 5),
  Duration suspiciousJumpThreshold = const Duration(hours: 1),
}) {
  final monotonicDeltaMs = current.monotonicMs - previous.monotonicMs;
  if (monotonicDeltaMs < 0) {
    return ClockJudgement.reboot;
  }

  final wallDeltaMs = current.wallMs - previous.wallMs;
  if (wallDeltaMs < -normalTolerance.inMilliseconds) {
    return ClockJudgement.rewind;
  }

  final driftMs = wallDeltaMs - monotonicDeltaMs;
  if (driftMs > suspiciousJumpThreshold.inMilliseconds) {
    return ClockJudgement.suspiciousForwardJump;
  }

  return ClockJudgement.normal;
}