xmr-remote-nodes/utils/human_readable.go

48 lines
1.2 KiB
Go
Raw Normal View History

package utils
2024-11-01 22:31:34 +07:00
import (
"fmt"
"strconv"
"time"
)
2024-11-01 22:31:34 +07:00
// Convert the float to a string, trimming unnecessary zeros
func FormatFloat(f float64) string {
2024-11-01 22:31:34 +07:00
return strconv.FormatFloat(f, 'f', -1, 64)
}
// TimeSince converts an int64 timestamp to a relative time string
func TimeSince(timestamp int64) string {
var duration time.Duration
var suffix string
t := time.Unix(timestamp, 0)
if t.After(time.Now()) {
duration = time.Until(t)
suffix = "from now"
} else {
duration = time.Since(t)
suffix = "ago"
}
switch {
case duration < time.Minute:
return fmt.Sprintf("%ds %s", int(duration.Seconds()), suffix)
case duration < time.Hour:
return fmt.Sprintf("%dm %s", int(duration.Minutes()), suffix)
case duration < time.Hour*24:
return fmt.Sprintf("%dh %s", int(duration.Hours()), suffix)
case duration < time.Hour*24*7:
return fmt.Sprintf("%dd %s", int(duration.Hours()/24), suffix)
case duration < time.Hour*24*30:
return fmt.Sprintf("%dw %s", int(duration.Hours()/(24*7)), suffix)
default:
months := int(duration.Hours() / (24 * 30))
if months == 1 {
return fmt.Sprintf("1 month %s", suffix)
}
return fmt.Sprintf("%d months %s", months, suffix)
}
}