Skip to content

Commit

Permalink
pluralize
Browse files Browse the repository at this point in the history
  • Loading branch information
Szer committed Oct 4, 2023
1 parent 7ed0505 commit 6d0435f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/VahterBanBot/Utils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ let prependUsername (s: string) =
s
else "@" + s

let pluralize (n: float) (s: string) =
if n < 2.0 then
""
else
"s"

let timeSpanAsHumanReadable (ts: TimeSpan) =
let totalSeconds = ts.TotalSeconds
if totalSeconds < 60.0 then
$"%.0f{totalSeconds} seconds"
$"%.0f{totalSeconds} second{pluralize totalSeconds}"
elif totalSeconds < 3600.0 then
$"%.0f{ts.TotalMinutes} minutes"
$"%.0f{ts.TotalMinutes} minute{pluralize ts.TotalMinutes}"
elif totalSeconds < 86400.0 then
$"%.0f{ts.TotalHours} hours"
$"%.0f{ts.TotalHours} hour{pluralize ts.TotalHours}"
else
$"%.0f{ts.TotalDays} days"
$"%.0f{ts.TotalDays} day{pluralize ts.TotalDays}"

0 comments on commit 6d0435f

Please sign in to comment.