From bafbf0c1a03fca794bc57602cf93427a7681c727 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Natal=C3=AD=20Paura?=
 <30585029+natilou@users.noreply.github.com>
Date: Fri, 19 May 2023 07:01:54 -0300
Subject: [PATCH] Improve purgeTime display in web page (#558)

- changing the line `purgeTime = s.purgeDays.String()` to use a function that formats the days like this: "N days" or "1 day"
- adding the function `formatDurationDays` in utils.go file

Fixes #557

Co-authored-by: Andrea Spacca <andrea.spacca@gmail.com>
---
 server/handlers.go | 2 +-
 server/utils.go    | 9 +++++++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/server/handlers.go b/server/handlers.go
index fb187c37..61dda738 100644
--- a/server/handlers.go
+++ b/server/handlers.go
@@ -374,7 +374,7 @@ func (s *Server) viewHandler(w http.ResponseWriter, r *http.Request) {
 
 	purgeTime := ""
 	if s.purgeDays > 0 {
-		purgeTime = s.purgeDays.String()
+		purgeTime = formatDurationDays(s.purgeDays)
 	}
 
 	data := struct {
diff --git a/server/utils.go b/server/utils.go
index 7bce32c3..4ff6d6ab 100644
--- a/server/utils.go
+++ b/server/utils.go
@@ -32,6 +32,7 @@ import (
 	"net/http"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/golang/gddo/httputil/header"
 )
@@ -233,3 +234,11 @@ func formatSize(size int64) string {
 	getSuffix := suffixes[int(math.Floor(base))]
 	return fmt.Sprintf("%s %s", strconv.FormatFloat(newVal, 'f', -1, 64), getSuffix)
 }
+
+func formatDurationDays(durationDays time.Duration) string {
+	days := int(durationDays.Hours() / 24)
+	if days == 1 {
+		return fmt.Sprintf("%d day", days)
+	}
+	return fmt.Sprintf("%d days", days)
+}
-- 
GitLab