Refactor duration formatting

This commit is contained in:
r 2022-05-10 17:27:33 +00:00
parent 4ad71587d8
commit fcbe489138

View file

@ -73,44 +73,41 @@ func displayInteractionCount(c int64) string {
return "" return ""
} }
func durToStr(s int64) string { func durUnit(s int64) (dur int64, unit string) {
if s < 60 { if s < 60 {
return strconv.Itoa(int(s)) + "s" if s < 0 {
s = 0
}
return s, "s"
} }
m := s / 60 m := s / 60
if m < 60*2 {
return strconv.Itoa(int(m)) + "m"
}
h := m / 60 h := m / 60
if h < 24*2 { if h < 2 {
return strconv.Itoa(int(h)) + "h" return m, "m"
} }
d := h / 24 d := h / 24
if d < 30*2 { if d < 2 {
return strconv.Itoa(int(d)) + "d" return h, "h"
} }
mo := d / 30 mo := d / 30
if mo < 12*2 { if mo < 2 {
return strconv.Itoa(int(mo)) + "mo" return d, "d"
} }
y := d / 365 y := d / 365
return strconv.Itoa(int(y)) + "y" if y < 2 {
return mo, "mo"
}
return y, "y"
} }
func timeSince(t time.Time) string { func timeSince(t time.Time) string {
d := time.Now().Unix() - t.Unix() d, u := durUnit(time.Now().Unix() - t.Unix())
if d < 0 { return strconv.FormatInt(d, 10) + u
d = 0
}
return durToStr(d)
} }
func timeUntil(t time.Time) string { func timeUntil(t time.Time) string {
d := t.Unix() - time.Now().Unix() d, u := durUnit(t.Unix() - time.Now().Unix())
if d < 0 { return strconv.FormatInt(d, 10) + u
d = 0
}
return durToStr(d)
} }
func formatTimeRFC3339(t time.Time) string { func formatTimeRFC3339(t time.Time) string {