commons clockwork

-- v0.90 (ADR 0114): the `Instant` primitive — absolute time. No literal;
-- minted by `Clock.now()`, built from `Int` via `fromEpochMillis`. Arithmetic
-- composes with `Duration`; comparison and ordering work; the kernel escapes
-- to raw epoch millis.

fn horizon(now: Instant) -> Instant {
  now + 1.hours
}

fn rewind(now: Instant) -> Instant {
  now - 30.minutes
}

fn elapsed(start: Instant, end: Instant) -> Duration {
  end - start
}

fn isBefore(a: Instant, b: Instant) -> Bool {
  a < b
}

fn raw(t: Instant) -> Int {
  t.toEpochMillis()
}

fn fromMillis(n: Int) -> Instant {
  Instant.fromEpochMillis(n)
}

fn earliest(ts: List[Instant]) -> Option[Instant] {
  ts.min((t) => t)
}

fn chronological(ts: List[Instant]) -> List[Instant] {
  ts.sortBy((t) => t)
}
