Simple better format for Duration toString

This commit is contained in:
Ozzie Gooen 2022-05-22 22:11:28 -04:00
parent fff2f5da59
commit 6af2e242f8
2 changed files with 14 additions and 10 deletions

View File

@ -849,23 +849,27 @@ module Duration = {
let toHours = (t: t): float => t /. hour
let toDays = (t: t): float => t /. day
let toYears = (t: t): float => t /. year
let toString = (t: t): string => {
let shouldPluralize = f => f != 1.0
let display = (f: float, s: string) =>
`${Float.with3DigitsPrecision(f)} ${s}${shouldPluralize(f) ? "s" : ""}`
if t >= year {
Float.with3DigitsPrecision(t /. year) ++ " years"
display(t /. year, "year")
} else if t >= day {
Float.with3DigitsPrecision(t /. day) ++ " days"
display(t /. day, "day")
} else if t >= hour {
Float.with3DigitsPrecision(t /. hour) ++ " hours"
display(t /. hour, "hour")
} else if t >= minute {
Float.with3DigitsPrecision(t /. minute) ++ " minutes"
display(t /. minute, "minute")
} else {
Float.toFixed(t) ++ "ms"
}
}
let add = (t1, t2): t => t1 +. t2
let subtract = (t1, t2): t => t1 -. t2
let multiply = (t1, t2): t => t1 *. t2
let divide = (t1, t2): t => t1 /. t2
let add = (t1: t, t2: t): t => t1 +. t2
let subtract = (t1: t, t2: t): t => t1 -. t2
let multiply = (t1: t, t2: t): t => t1 *. t2
let divide = (t1: t, t2: t): t => t1 /. t2
}
module Date = {