type timestamp = int let timestamp_of_unix_seconds s = s let timestamp_to_unix_seconds t = t type duration = int let hours n = n * 3600 let days n = n * 86_400 let duration_to_seconds d = d let elapsed ~since ~now = Int.max 0 (now - since) let pp_duration ppf d = if d mod 86_400 = 0 && d <> 0 then Format.fprintf ppf "%dd" (d / 86_400) else if d mod 3600 = 0 && d <> 0 then Format.fprintf ppf "%dh" (d / 3600) else Format.fprintf ppf "%ds" d type readiness = | Ready | Recovering of { rested : duration; recommended : duration } let evaluate_readiness ~elapsed ~recommended = if elapsed >= recommended then Ready else Recovering { rested = elapsed; recommended } let is_ready = function Ready -> true | Recovering _ -> false let pp_readiness ppf = function | Ready -> Format.pp_print_string ppf "ready" | Recovering { rested; recommended } -> Format.fprintf ppf "recovering (%a of %a)" pp_duration rested pp_duration recommended type basis = | Recovered | Overridden of { rested : duration; recommended : duration } type clearance = basis let clear = function Ready -> Some Recovered | Recovering _ -> None let override readiness = match readiness with | Ready -> Recovered | Recovering { rested; recommended } -> Overridden { rested; recommended } let basis c = c