diff options
author | r <r@freesoftwareextremist.com> | 2022-05-10 17:27:33 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2022-05-10 17:27:33 +0000 |
commit | fcbe489138ff60a0bba716bb6c7684327d78558d (patch) | |
tree | b740deeba73f3e8dc71610ce97959c501a5eefa0 | |
parent | 4ad71587d821d8586d1fe648ad15f601a318662f (diff) | |
download | bloat-fcbe489138ff60a0bba716bb6c7684327d78558d.tar.gz bloat-fcbe489138ff60a0bba716bb6c7684327d78558d.zip |
Refactor duration formatting
-rw-r--r-- | renderer/renderer.go | 41 |
1 files changed, 19 insertions, 22 deletions
diff --git a/renderer/renderer.go b/renderer/renderer.go index c6b3189..7afeb14 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -73,44 +73,41 @@ func displayInteractionCount(c int64) string { return "" } -func durToStr(s int64) string { +func durUnit(s int64) (dur int64, unit string) { if s < 60 { - return strconv.Itoa(int(s)) + "s" + if s < 0 { + s = 0 + } + return s, "s" } m := s / 60 - if m < 60*2 { - return strconv.Itoa(int(m)) + "m" - } h := m / 60 - if h < 24*2 { - return strconv.Itoa(int(h)) + "h" + if h < 2 { + return m, "m" } d := h / 24 - if d < 30*2 { - return strconv.Itoa(int(d)) + "d" + if d < 2 { + return h, "h" } mo := d / 30 - if mo < 12*2 { - return strconv.Itoa(int(mo)) + "mo" + if mo < 2 { + return d, "d" } y := d / 365 - return strconv.Itoa(int(y)) + "y" + if y < 2 { + return mo, "mo" + } + return y, "y" } func timeSince(t time.Time) string { - d := time.Now().Unix() - t.Unix() - if d < 0 { - d = 0 - } - return durToStr(d) + d, u := durUnit(time.Now().Unix() - t.Unix()) + return strconv.FormatInt(d, 10) + u } func timeUntil(t time.Time) string { - d := t.Unix() - time.Now().Unix() - if d < 0 { - d = 0 - } - return durToStr(d) + d, u := durUnit(t.Unix() - time.Now().Unix()) + return strconv.FormatInt(d, 10) + u } func formatTimeRFC3339(t time.Time) string { |