lib: Print nicely rounded durations (#6756)

This commit is contained in:
Simon Frei
2020-06-18 10:55:41 +02:00
committed by GitHub
parent 4812fd3ec1
commit 8cf9d91ed4
3 changed files with 20 additions and 2 deletions
+17
View File
@@ -14,6 +14,7 @@ import (
"reflect"
"strconv"
"strings"
"time"
"github.com/syncthing/syncthing/lib/sync"
@@ -314,3 +315,19 @@ func CallWithContext(ctx context.Context, fn func() error) error {
return ctx.Err()
}
}
func NiceDurationString(d time.Duration) string {
switch {
case d > 24*time.Hour:
d = d.Round(time.Hour)
case d > time.Hour:
d = d.Round(time.Minute)
case d > time.Minute:
d = d.Round(time.Second)
case d > time.Second:
d = d.Round(time.Millisecond)
case d > time.Millisecond:
d = d.Round(time.Microsecond)
}
return d.String()
}