mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-05-07 00:37:36 +00:00
36 lines
780 B
Go
36 lines
780 B
Go
package utils
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
// FormatDuration returns a short human-readable elapsed-time string.
|
|
func FormatDuration(d time.Duration) string {
|
|
if d < time.Minute {
|
|
return "just now"
|
|
}
|
|
if d < time.Hour {
|
|
return formatUnit(int(d.Minutes()), "minute")
|
|
}
|
|
if d < 24*time.Hour {
|
|
return formatUnit(int(d.Hours()), "hour")
|
|
}
|
|
return formatUnit(int(d.Hours()/24), "day")
|
|
}
|
|
|
|
// FormatDurationAgo returns a short human-readable elapsed-time string with an "ago" suffix.
|
|
func FormatDurationAgo(d time.Duration) string {
|
|
formatted := FormatDuration(d)
|
|
if formatted == "just now" {
|
|
return formatted
|
|
}
|
|
return formatted + " ago"
|
|
}
|
|
|
|
func formatUnit(n int, unit string) string {
|
|
if n == 1 {
|
|
return "1 " + unit
|
|
}
|
|
return strconv.Itoa(n) + " " + unit + "s"
|
|
}
|