Skip to content

Commit

Permalink
Improve purgeTime display in web page (dutchcoders#558)
Browse files Browse the repository at this point in the history
- 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 dutchcoders#557

Co-authored-by: Andrea Spacca <[email protected]>
  • Loading branch information
natilou and aspacca authored May 19, 2023
1 parent df0d04f commit bafbf0c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 9 additions & 0 deletions server/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"net/http"
"strconv"
"strings"
"time"

"github.com/golang/gddo/httputil/header"
)
Expand Down Expand Up @@ -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)
}

0 comments on commit bafbf0c

Please sign in to comment.