mirror of https://github.com/containrrr/watchtower
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
908 B
Go
48 lines
908 B
Go
package util
|
|
|
|
import (
|
|
"math"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func FormatDuration(d time.Duration) string {
|
|
sb := strings.Builder{}
|
|
|
|
hours := int64(d.Hours())
|
|
minutes := int64(math.Mod(d.Minutes(), 60))
|
|
seconds := int64(math.Mod(d.Seconds(), 60))
|
|
|
|
if hours == 1 {
|
|
sb.WriteString("1 hour")
|
|
} else if hours != 0 {
|
|
sb.WriteString(strconv.FormatInt(hours, 10))
|
|
sb.WriteString(" hours")
|
|
}
|
|
|
|
if hours != 0 && (seconds != 0 || minutes != 0) {
|
|
sb.WriteString(", ")
|
|
}
|
|
|
|
if minutes == 1 {
|
|
sb.WriteString("1 minute")
|
|
} else if minutes != 0 {
|
|
sb.WriteString(strconv.FormatInt(minutes, 10))
|
|
sb.WriteString(" minutes")
|
|
}
|
|
|
|
if minutes != 0 && (seconds != 0) {
|
|
sb.WriteString(", ")
|
|
}
|
|
|
|
if seconds == 1 {
|
|
sb.WriteString("1 second")
|
|
} else if seconds != 0 || (hours == 0 && minutes == 0) {
|
|
sb.WriteString(strconv.FormatInt(seconds, 10))
|
|
sb.WriteString(" seconds")
|
|
}
|
|
|
|
return sb.String()
|
|
}
|