29 lines
504 B
Go
29 lines
504 B
Go
|
package utils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type timeUtils struct{}
|
||
|
|
||
|
func TimeUtils() *timeUtils {
|
||
|
return &timeUtils{}
|
||
|
}
|
||
|
|
||
|
// DurationString Duration转自定义String
|
||
|
func (tu timeUtils) DurationString(d time.Duration) string {
|
||
|
if d.Seconds() == 0 {
|
||
|
return "00:00:00"
|
||
|
}
|
||
|
s := d.String()
|
||
|
s = strings.ReplaceAll(s, "h", "小时")
|
||
|
s = strings.ReplaceAll(s, "m", "分")
|
||
|
idx := strings.Index(s, ".")
|
||
|
if idx == -1 {
|
||
|
return fmt.Sprintf("%s秒", s)
|
||
|
}
|
||
|
return fmt.Sprintf("%s秒", s[:idx])
|
||
|
}
|