2022-08-14 21:40:57 +00:00
|
|
|
package utility
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-22 01:12:34 +00:00
|
|
|
"strconv"
|
2022-08-14 21:40:57 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2024-10-06 03:24:45 +00:00
|
|
|
// NextMonth takes an input time (usually time.Now) and will return the next month on the given day.
|
|
|
|
//
|
|
|
|
// Example, NextMonth from any time in April should return e.g. May 10th.
|
|
|
|
func NextMonth(now time.Time, day int) time.Time {
|
|
|
|
var (
|
|
|
|
year, month, _ = now.Date()
|
|
|
|
nextMonth = month + 1
|
|
|
|
)
|
|
|
|
if nextMonth > 12 {
|
|
|
|
nextMonth = 1
|
|
|
|
year++
|
|
|
|
}
|
|
|
|
|
|
|
|
return time.Date(year, nextMonth, day, 0, 0, 0, 0, now.Location())
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
// FormatDurationCoarse returns a pretty printed duration with coarse granularity.
|
|
|
|
func FormatDurationCoarse(duration time.Duration) string {
|
2022-12-21 03:15:45 +00:00
|
|
|
// Negative durations (e.g. future dates) should work too.
|
|
|
|
if duration < 0 {
|
|
|
|
duration *= -1
|
|
|
|
}
|
|
|
|
|
2022-08-14 21:40:57 +00:00
|
|
|
var result = func(text string, v int64) string {
|
|
|
|
if v == 1 {
|
|
|
|
text = strings.TrimSuffix(text, "s")
|
|
|
|
}
|
|
|
|
return fmt.Sprintf(text, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration.Seconds() < 60.0 {
|
|
|
|
return result("%d seconds", int64(duration.Seconds()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration.Minutes() < 60.0 {
|
|
|
|
return result("%d minutes", int64(duration.Minutes()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if duration.Hours() < 24.0 {
|
|
|
|
return result("%d hours", int64(duration.Hours()))
|
|
|
|
}
|
|
|
|
|
|
|
|
days := int64(duration.Hours() / 24)
|
|
|
|
if days < 30 {
|
|
|
|
return result("%d days", days)
|
|
|
|
}
|
|
|
|
|
|
|
|
months := int64(days / 30)
|
2023-12-22 01:12:34 +00:00
|
|
|
if months < 12 {
|
2022-08-14 21:40:57 +00:00
|
|
|
return result("%d months", months)
|
|
|
|
}
|
|
|
|
|
2023-12-22 01:12:34 +00:00
|
|
|
// Over one year: start to show it as a floating point number of years (e.g. "1.2 years")
|
|
|
|
years := float64(days) / 365
|
|
|
|
s := strconv.FormatFloat(years, 'f', 1, 64)
|
|
|
|
if strings.HasSuffix(s, ".0") {
|
|
|
|
y, _ := strconv.Atoi(strings.Split(s, ".")[0])
|
|
|
|
return result("%d years", int64(y))
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%s years", s)
|
2022-08-14 21:40:57 +00:00
|
|
|
}
|