mirror of
https://github.com/containrrr/watchtower.git
synced 2025-12-16 15:10:12 +01:00
feat(api): implement new api handler
This commit is contained in:
parent
72e437f173
commit
47091761a5
17 changed files with 571 additions and 294 deletions
47
internal/util/duration.go
Normal file
47
internal/util/duration.go
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
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()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue