Time.res
1/**
2 * Convert seconds to a human-readable format: "XhYm" or "Ym" when less than 1 hour
3 * Truncates remaining seconds (rounds down).
4 *
5 * @param time_sec Time in seconds
6 * @returns String formatted as "{hours}h{minutes}m" or "{minutes}m"
7 *
8 * Example: 13322.0 -> "3h42m"
9 * - 13322 / 3600 = 3.7... hours -> 3 hours
10 * - 13322 - 10800 = 2522 remaining seconds
11 * - 2522 / 60 = 42.0... minutes -> 42 minutes
12 * Example: 2520.0 -> "42m" (less than 1 hour)
13 */
14let humanize = (time_sec: float): string => {
15 let secondsPerHour = 3600.0
16 let secondsPerMinute = 60.0
17
18 let hours = Math.floor(time_sec /. secondsPerHour)
19 let remainingSeconds = time_sec -. (hours *. secondsPerHour)
20 let minutes = Math.floor(remainingSeconds /. secondsPerMinute)
21
22 if hours > 0.0 {
23 `${Float.toString(hours)}h${Float.toString(minutes)}m`
24 } else {
25 `${Float.toString(minutes)}m`
26 }
27}