package com.example.aiapp import java.time.Duration import java.time.OffsetDateTime /** * How long is left in a usage window. * * Shared by the session bar and the usage screen: the arithmetic is the same in both and only the * sentence around it differs, so this returns the span on its own and leaves the wording to the * caller. */ /** "1d 4h", "3h 12m", "12m" -- the span alone, with no leading or trailing words. */ fun formatSpan(until: Duration): String = when { until.toHours() >= 24 -> "${until.toDays()}d ${until.toHours() % 24}h" until.toHours() > 0 -> "${until.toHours()}h ${until.toMinutes() % 60}m" else -> "${until.toMinutes()}m" } /** * Time from [now] until [resetsAt], or null when that timestamp cannot be read. * * Null rather than a zero duration, because a string this app failed to parse is not a window that * has just run out: a caller given zero for both would tell the reader to refresh on the strength * of something nobody measured. * * [now] is a parameter rather than read here so a caller can drive it from state and have the * countdown recompute on its own schedule. */ fun remainingUntil(resetsAt: String, now: OffsetDateTime): Duration? = try { Duration.between(now, OffsetDateTime.parse(resetsAt)) } catch (_: Exception) { null }