2021-08-13 20:28:15 +00:00
|
|
|
package utils
|
2021-08-11 12:27:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-08-13 20:28:15 +00:00
|
|
|
func FormatSeconds(s time.Duration) string {
|
2021-08-11 12:27:54 +00:00
|
|
|
hours := Round(s.Hours())
|
|
|
|
minutes := Round(s.Minutes())
|
|
|
|
seconds := Round(s.Seconds())
|
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
var (
|
|
|
|
days int
|
|
|
|
format string
|
|
|
|
)
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
for hours/24 > 0 {
|
|
|
|
days++
|
|
|
|
hours -= 24
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if days > 0 {
|
2021-08-12 20:47:54 +00:00
|
|
|
format += strconv.Itoa(days) + "d"
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if hours > 0 {
|
2021-08-12 20:47:54 +00:00
|
|
|
if len(format) >= 1 {
|
|
|
|
format += " "
|
|
|
|
}
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
format += strconv.Itoa(hours) + "h"
|
|
|
|
} else if days > 0 {
|
|
|
|
hours = days * 24
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
minutes -= Round(s.Hours()) * 60
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
if minutes > 0 {
|
|
|
|
if len(format) >= 1 {
|
|
|
|
format += " "
|
|
|
|
}
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
format += strconv.Itoa(minutes) + "m"
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
seconds -= Round(s.Minutes()) * 60
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
if seconds > 0 {
|
|
|
|
if len(format) >= 1 {
|
|
|
|
format += " "
|
|
|
|
}
|
2021-08-11 12:27:54 +00:00
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
format += strconv.Itoa(seconds) + "s"
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|
|
|
|
|
2021-08-13 20:28:15 +00:00
|
|
|
if len(format) == 0 {
|
|
|
|
format = "0s"
|
|
|
|
}
|
|
|
|
|
2021-08-12 20:47:54 +00:00
|
|
|
return format
|
2021-08-11 12:27:54 +00:00
|
|
|
}
|