From 2a014716216d445953b4832958e80c739bb02c2c Mon Sep 17 00:00:00 2001 From: r Date: Wed, 30 Mar 2022 16:58:31 +0000 Subject: Fix duration overflow time.Sub() returns int64 duration in nanoseconds, which can only represent around 290 years. --- renderer/renderer.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/renderer/renderer.go b/renderer/renderer.go index 6e50270..f1595a5 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -73,16 +73,15 @@ func displayInteractionCount(c int64) string { return "" } -func DurToStr(dur time.Duration) string { - s := dur.Seconds() +func durToStr(s int64) string { if s < 60 { return strconv.Itoa(int(s)) + "s" } - m := dur.Minutes() + m := s / 60 if m < 60*2 { return strconv.Itoa(int(m)) + "m" } - h := dur.Hours() + h := m / 60 if h < 24*2 { return strconv.Itoa(int(h)) + "h" } @@ -94,24 +93,24 @@ func DurToStr(dur time.Duration) string { if mo < 12*2 { return strconv.Itoa(int(mo)) + "mo" } - y := mo / 12 + y := d / 365 return strconv.Itoa(int(y)) + "y" } func timeSince(t time.Time) string { - d := time.Since(t) + d := time.Now().Unix() - t.Unix() if d < 0 { d = 0 } - return DurToStr(d) + return durToStr(d) } func timeUntil(t time.Time) string { - d := time.Until(t) + d := t.Unix() - time.Now().Unix() if d < 0 { d = 0 } - return DurToStr(d) + return durToStr(d) } func formatTimeRFC3339(t time.Time) string { -- cgit v1.2.3