2024-11-06 17:11:16 +07:00
|
|
|
package utils
|
2024-11-01 22:31:34 +07:00
|
|
|
|
2024-11-01 23:03:10 +07:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
2024-11-01 22:31:34 +07:00
|
|
|
|
|
|
|
// Convert the float to a string, trimming unnecessary zeros
|
2024-11-06 17:11:16 +07:00
|
|
|
func FormatFloat(f float64) string {
|
2024-11-01 22:31:34 +07:00
|
|
|
return strconv.FormatFloat(f, 'f', -1, 64)
|
|
|
|
}
|
2024-11-01 23:03:10 +07:00
|
|
|
|
|
|
|
// TimeSince converts an int64 timestamp to a relative time string
|
2024-11-06 17:11:16 +07:00
|
|
|
func TimeSince(timestamp int64) string {
|
2024-11-01 23:03:10 +07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|